Skip to content

Commit eac7496

Browse files
committed
deduplicate code of single-widget-presenting locker plugins
1 parent f4414cc commit eac7496

10 files changed

Lines changed: 134 additions & 189 deletions

File tree

‎src/locker/plugin/battery.cpp‎

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,5 @@
1-
#include <memory>
2-
3-
#include "lockergrid.hpp"
4-
#include "timedrevealer.hpp"
51
#include "battery.hpp"
62

7-
WayfireLockerBatteryPluginWidget::WayfireLockerBatteryPluginWidget() :
8-
WayfireLockerTimedRevealer("locker/battery_always"),
9-
battery("locker")
10-
{
11-
set_child(battery);
12-
}
13-
14-
void WayfireLockerBatteryPlugin::add_output(std::string id, std::shared_ptr<WayfireLockerGrid> grid)
15-
{
16-
widgets.emplace(id, new WayfireLockerBatteryPluginWidget());
17-
auto widget = widgets[id];
18-
19-
if (!show_state)
20-
{
21-
widget->hide();
22-
}
23-
24-
grid->attach(*widget, (std::string)position);
25-
}
26-
27-
void WayfireLockerBatteryPlugin::remove_output(std::string id, std::shared_ptr<WayfireLockerGrid> grid)
28-
{
29-
grid->remove(*widgets[id]);
30-
widgets.erase(id);
31-
}
32-
33-
void WayfireLockerBatteryPlugin::hide()
34-
{
35-
show_state = false;
36-
for (auto& it : widgets)
37-
{
38-
it.second->hide();
39-
}
40-
}
41-
42-
void WayfireLockerBatteryPlugin::show()
43-
{
44-
show_state = true;
45-
for (auto& it : widgets)
46-
{
47-
it.second->show();
48-
}
49-
}
50-
513
WayfireLockerBatteryPlugin::WayfireLockerBatteryPlugin() :
52-
WayfireLockerPlugin("locker/battery")
4+
WayfireLockerMultiOutputPlugin("locker/battery")
535
{}

‎src/locker/plugin/battery.hpp‎

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,28 @@
11
#pragma once
22

3-
#include "plugin.hpp"
4-
#include "lockergrid.hpp"
5-
#include "timedrevealer.hpp"
3+
#include "multi-output-timed-plugin.hpp"
64
#include "widget-utils/battery.hpp"
75

8-
class WayfireLockerBatteryPluginWidget : public WayfireLockerTimedRevealer
6+
class WayfireLockerBatteryPluginWidget : public WayfireLockerTimedWidget<ShellBattery>
97
{
108
public:
11-
ShellBattery battery;
12-
WayfireLockerBatteryPluginWidget();
9+
WayfireLockerBatteryPluginWidget() :
10+
WayfireLockerTimedWidget("locker/battery_always")
11+
{}
1312
};
1413

15-
class WayfireLockerBatteryPlugin : public WayfireLockerPlugin
14+
class WayfireLockerBatteryPlugin :
15+
public WayfireLockerMultiOutputPlugin<WayfireLockerBatteryPluginWidget>
1616
{
1717
private:
1818
sigc::connection signal;
1919

20+
protected:
21+
std::shared_ptr<WayfireLockerBatteryPluginWidget> create_widget() override
22+
{
23+
return std::make_shared<WayfireLockerBatteryPluginWidget>();
24+
}
25+
2026
public:
2127
WayfireLockerBatteryPlugin();
22-
void add_output(std::string id, std::shared_ptr<WayfireLockerGrid> grid) override;
23-
void remove_output(std::string id, std::shared_ptr<WayfireLockerGrid> grid) override;
24-
void init() override
25-
{}
26-
void deinit() override
27-
{}
28-
void hide();
29-
void show();
30-
bool show_state = true;
31-
32-
std::map<std::string, std::shared_ptr<WayfireLockerBatteryPluginWidget>> widgets;
3328
};

‎src/locker/plugin/clock.cpp‎

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,5 @@
1-
#include <memory>
2-
#include <glibmm.h>
3-
#include <gtkmm/box.h>
4-
5-
#include "lockergrid.hpp"
6-
#include "timedrevealer.hpp"
71
#include "clock.hpp"
82

9-
10-
WayfireLockerClockPluginWidget::WayfireLockerClockPluginWidget() :
11-
WayfireLockerTimedRevealer("locker/clock_always")
12-
{
13-
set_child(label);
14-
label.add_css_class("clock");
15-
}
16-
17-
void WayfireLockerClockPlugin::add_output(std::string id, std::shared_ptr<WayfireLockerGrid> grid)
18-
{
19-
widgets.emplace(id, new WayfireLockerClockPluginWidget());
20-
auto widget = widgets[id];
21-
22-
grid->attach(*widget, position);
23-
}
24-
25-
void WayfireLockerClockPlugin::remove_output(std::string id, std::shared_ptr<WayfireLockerGrid> grid)
26-
{
27-
grid->remove(*widgets[id]);
28-
widgets.erase(id);
29-
}
30-
313
WayfireLockerClockPlugin::WayfireLockerClockPlugin() :
32-
WayfireLockerPlugin("locker/clock")
4+
WayfireLockerMultiOutputPlugin("locker/clock")
335
{}

‎src/locker/plugin/clock.hpp‎

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,25 @@
11
#pragma once
22

3-
#include <unordered_map>
4-
5-
#include "plugin.hpp"
6-
#include "timedrevealer.hpp"
7-
#include "lockergrid.hpp"
3+
#include "multi-output-timed-plugin.hpp"
84
#include "widget-utils/clock.hpp"
95

10-
class WayfireLockerClockPluginWidget : public WayfireLockerTimedRevealer
6+
class WayfireLockerClockPluginWidget : public WayfireLockerTimedWidget<ShellClock>
117
{
128
public:
13-
ShellClock label{"locker/clock_format"};
14-
WayfireLockerClockPluginWidget();
9+
WayfireLockerClockPluginWidget() :
10+
WayfireLockerTimedWidget("locker/clock_always", "clock")
11+
{}
1512
};
1613

17-
class WayfireLockerClockPlugin : public WayfireLockerPlugin
14+
class WayfireLockerClockPlugin :
15+
public WayfireLockerMultiOutputPlugin<WayfireLockerClockPluginWidget>
1816
{
17+
protected:
18+
std::shared_ptr<WayfireLockerClockPluginWidget> create_widget() override
19+
{
20+
return std::make_shared<WayfireLockerClockPluginWidget>();
21+
}
22+
1923
public:
2024
WayfireLockerClockPlugin();
21-
void add_output(std::string id, std::shared_ptr<WayfireLockerGrid> grid) override;
22-
void remove_output(std::string id, std::shared_ptr<WayfireLockerGrid> grid) override;
23-
void init() override
24-
{}
25-
void deinit() override
26-
{}
27-
28-
std::unordered_map<std::string, std::shared_ptr<WayfireLockerClockPluginWidget>> widgets;
2925
};
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#pragma once
2+
3+
#include <unordered_map>
4+
#include <memory>
5+
6+
#include "plugin.hpp"
7+
#include "timedrevealer.hpp"
8+
#include "lockergrid.hpp"
9+
10+
template<class WidgetT>
11+
class WayfireLockerMultiOutputPlugin : public WayfireLockerPlugin
12+
{
13+
protected:
14+
std::unordered_map<std::string, std::shared_ptr<WidgetT>> widgets;
15+
bool shown = true;
16+
17+
virtual std::shared_ptr<WidgetT> create_widget() = 0;
18+
19+
public:
20+
WayfireLockerMultiOutputPlugin(std::string prefix) :
21+
WayfireLockerPlugin(prefix)
22+
{}
23+
24+
void add_output(std::string id, std::shared_ptr<WayfireLockerGrid> grid) override
25+
{
26+
auto widget = create_widget();
27+
widgets.emplace(id, widget);
28+
29+
if (!shown)
30+
{
31+
widget->hide();
32+
}
33+
34+
grid->attach(*widget, position);
35+
}
36+
37+
void remove_output(std::string id, std::shared_ptr<WayfireLockerGrid> grid) override
38+
{
39+
auto it = widgets.find(id);
40+
if (it == widgets.end())
41+
{
42+
return;
43+
}
44+
45+
grid->remove(*it->second);
46+
widgets.erase(it);
47+
}
48+
49+
void hide()
50+
{
51+
shown = false;
52+
for (auto& it : widgets)
53+
{
54+
it.second->hide();
55+
}
56+
}
57+
58+
void show()
59+
{
60+
shown = true;
61+
for (auto& it : widgets)
62+
{
63+
it.second->show();
64+
}
65+
}
66+
67+
void init() override
68+
{}
69+
void deinit() override
70+
{}
71+
};
72+
73+
template<class InnerWidgetT>
74+
class WayfireLockerTimedWidget : public WayfireLockerTimedRevealer
75+
{
76+
public:
77+
InnerWidgetT inner;
78+
79+
WayfireLockerTimedWidget(std::string always_option, const char *css_class = nullptr) :
80+
WayfireLockerTimedRevealer(always_option),
81+
inner("locker")
82+
{
83+
set_child(inner);
84+
if (css_class != nullptr)
85+
{
86+
inner.add_css_class(css_class);
87+
}
88+
}
89+
};

‎src/locker/plugin/weather.cpp‎

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,5 @@
1-
#include <memory>
2-
#include <glibmm.h>
3-
4-
#include "lockergrid.hpp"
51
#include "weather.hpp"
6-
#include "timedrevealer.hpp"
7-
8-
WayfireLockerWeatherPluginWidget::WayfireLockerWeatherPluginWidget() :
9-
WayfireLockerTimedRevealer("locker/weather_always")
10-
{
11-
set_child(weather);
12-
weather.add_css_class("weather");
13-
}
14-
15-
void WayfireLockerWeatherPlugin::add_output(std::string id, std::shared_ptr<WayfireLockerGrid> grid)
16-
{
17-
weather_widgets.emplace(id, new WayfireLockerWeatherPluginWidget());
18-
19-
auto weather_widget = weather_widgets[id];
20-
if (!shown)
21-
{
22-
weather_widget->hide();
23-
}
24-
25-
grid->attach(*weather_widget, position);
26-
}
27-
28-
void WayfireLockerWeatherPlugin::remove_output(std::string id, std::shared_ptr<WayfireLockerGrid> grid)
29-
{
30-
grid->remove(*weather_widgets[id]);
31-
weather_widgets.erase(id);
32-
}
332

343
WayfireLockerWeatherPlugin::WayfireLockerWeatherPlugin() :
35-
WayfireLockerPlugin("locker/weather")
4+
WayfireLockerMultiOutputPlugin("locker/weather")
365
{}
37-
38-
void WayfireLockerWeatherPlugin::hide()
39-
{
40-
for (auto& it : weather_widgets)
41-
{
42-
it.second->hide();
43-
}
44-
45-
shown = false;
46-
}
47-
48-
void WayfireLockerWeatherPlugin::show()
49-
{
50-
for (auto& it : weather_widgets)
51-
{
52-
it.second->show();
53-
}
54-
55-
shown = true;
56-
}

‎src/locker/plugin/weather.hpp‎

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,25 @@
11
#pragma once
2-
#include <unordered_map>
3-
4-
#include "plugin.hpp"
5-
#include "timedrevealer.hpp"
6-
#include "lockergrid.hpp"
72

3+
#include "multi-output-timed-plugin.hpp"
84
#include "widget-utils/weather.hpp"
95

10-
class WayfireLockerWeatherPluginWidget : public WayfireLockerTimedRevealer
6+
class WayfireLockerWeatherPluginWidget : public WayfireLockerTimedWidget<ShellWeather>
117
{
128
public:
13-
ShellWeather weather;
14-
WayfireLockerWeatherPluginWidget();
9+
WayfireLockerWeatherPluginWidget() :
10+
WayfireLockerTimedWidget("locker/weather_always", "weather")
11+
{}
1512
};
1613

17-
class WayfireLockerWeatherPlugin : public WayfireLockerPlugin
14+
class WayfireLockerWeatherPlugin :
15+
public WayfireLockerMultiOutputPlugin<WayfireLockerWeatherPluginWidget>
1816
{
17+
protected:
18+
std::shared_ptr<WayfireLockerWeatherPluginWidget> create_widget() override
19+
{
20+
return std::make_shared<WayfireLockerWeatherPluginWidget>();
21+
}
22+
1923
public:
2024
WayfireLockerWeatherPlugin();
21-
void add_output(std::string id, std::shared_ptr<WayfireLockerGrid> grid) override;
22-
void remove_output(std::string id, std::shared_ptr<WayfireLockerGrid> grid) override;
23-
void init() override
24-
{}
25-
void deinit() override
26-
{}
27-
28-
void hide();
29-
void show();
30-
31-
std::unordered_map<std::string, std::shared_ptr<WayfireLockerWeatherPluginWidget>> weather_widgets;
32-
bool shown = true;
3325
};

‎src/panel/widgets/weather.hpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
class WayfireWeather : public WayfireWidget
77
{
8-
ShellWeather weather;
8+
ShellWeather weather{"panel"};
99

1010
public:
1111
void init(Gtk::Box *container) override;

‎src/util/widget-utils/weather.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include "weather.hpp"
1010

1111

12-
ShellWeather::ShellWeather()
12+
ShellWeather::ShellWeather(const std::string& section)
1313
{
1414
set_orientation(Gtk::Orientation::HORIZONTAL);
1515

0 commit comments

Comments
 (0)