1
1
/*
2
2
* GTK-based button bar
3
- * Copyright (c) 2020 Piotr Miller
3
+ * Copyright (c) 2021 Piotr Miller
4
4
5
5
* Website: http://nwg.pl
6
6
* Project: https://github.com/nwg-piotr/nwg-launchers
14
14
#include " bar.h"
15
15
16
16
const char * const HELP_MESSAGE =
17
- " GTK button bar: nwgbar " VERSION_STR " (c) Piotr Miller & Contributors 2020 \n\n \
17
+ " GTK button bar: nwgbar " VERSION_STR " (c) Piotr Miller & Contributors 2021 \n\n \
18
18
Options:\n \
19
19
-h show this help message and exit\n \
20
20
-v arrange buttons vertically\n \
@@ -31,114 +31,118 @@ Options:\n\
31
31
-layer-shell-exclusive-zone {auto, valid integer (usually -1 or 0)}, default: auto\n " ;
32
32
33
33
int main (int argc, char *argv[]) {
34
- struct timeval tp;
35
- gettimeofday (&tp, NULL );
36
- long int start_ms = tp.tv_sec * 1000 + tp.tv_usec / 1000 ;
37
-
38
- create_pid_file_or_kill_pid (" nwgbar" );
39
-
40
- InputParser input (argc, argv);
41
- if (input.cmdOptionExists (" -h" )){
42
- std::cout << HELP_MESSAGE;
43
- std::exit (0 );
44
- }
34
+ try {
35
+ struct timeval tp;
36
+ gettimeofday (&tp, NULL );
37
+ long int start_ms = tp.tv_sec * 1000 + tp.tv_usec / 1000 ;
38
+
39
+ InputParser input (argc, argv);
40
+ if (input.cmdOptionExists (" -h" )){
41
+ std::cout << HELP_MESSAGE;
42
+ std::exit (0 );
43
+ }
45
44
46
- auto background_color = input.get_background_color (0.9 );
45
+ auto background_color = input.get_background_color (0.9 );
47
46
48
- auto config_dir = get_config_dir (" nwgbar" );
49
- if (!fs::is_directory (config_dir)) {
50
- Log::info (" Config dir not found, creating..." );
51
- fs::create_directories (config_dir);
52
- }
47
+ auto config_dir = get_config_dir (" nwgbar" );
48
+ if (!fs::is_directory (config_dir)) {
49
+ Log::info (" Config dir not found, creating..." );
50
+ fs::create_directories (config_dir);
51
+ }
53
52
54
- auto app = Gtk::Application::create ();
53
+ auto app = Gtk::Application::create ();
55
54
56
- auto provider = Gtk::CssProvider::create ();
57
- auto display = Gdk::Display::get_default ();
58
- auto screen = display->get_default_screen ();
59
- if (!provider || !display || !screen) {
60
- Log::error (" Failed to initialize GTK" );
61
- return EXIT_FAILURE;
62
- }
55
+ auto provider = Gtk::CssProvider::create ();
56
+ auto display = Gdk::Display::get_default ();
57
+ auto screen = display->get_default_screen ();
58
+ if (!provider || !display || !screen) {
59
+ Log::error (" Failed to initialize GTK" );
60
+ return EXIT_FAILURE;
61
+ }
63
62
64
- BarConfig config {
65
- input,
66
- screen
67
- };
63
+ BarConfig config {
64
+ input,
65
+ screen
66
+ };
67
+
68
+ // default or custom template
69
+ auto default_bar_file = config_dir / " bar.json" ;
70
+ auto custom_bar_file = config_dir / config.definition_file ;
71
+ // copy default anyway if not found
72
+ if (!fs::exists (default_bar_file)) {
73
+ try {
74
+ fs::copy_file (DATA_DIR_STR " /nwgbar/bar.json" , default_bar_file, fs::copy_options::overwrite_existing);
75
+ } catch (...) {
76
+ Log::error (" Failed copying default template" );
77
+ }
78
+ }
68
79
69
- // default or custom template
70
- auto default_bar_file = config_dir / " bar.json" ;
71
- auto custom_bar_file = config_dir / config.definition_file ;
72
- // copy default anyway if not found
73
- if (!fs::exists (default_bar_file)) {
80
+ ns::json bar_json;
74
81
try {
75
- fs::copy_file (DATA_DIR_STR " /nwgbar/bar.json" , default_bar_file, fs::copy_options::overwrite_existing);
76
- } catch (...) {
77
- Log::error (" Failed copying default template" );
82
+ bar_json = json_from_file (custom_bar_file);
83
+ } catch (...) {
84
+ Log::error (" Template file not found, using default" );
85
+ bar_json = json_from_file (default_bar_file);
78
86
}
79
- }
87
+ Log::info (bar_json. size (), " bar entries loaded " );
80
88
81
- ns::json bar_json;
82
- try {
83
- bar_json = json_from_file (custom_bar_file);
84
- } catch (...) {
85
- Log::error (" Template file not found, using default" );
86
- bar_json = json_from_file (default_bar_file);
87
- }
88
- Log::info (bar_json.size (), " bar entries loaded" );
89
-
90
- std::vector<BarEntry> bar_entries {};
91
- if (bar_json.size () > 0 ) {
92
- bar_entries = get_bar_entries (std::move (bar_json));
93
- }
94
-
95
- Gtk::StyleContext::add_provider_for_screen (screen, provider, GTK_STYLE_PROVIDER_PRIORITY_USER);
96
- {
97
- auto css_file = setup_css_file (" nwgbar" , config_dir, config.css_filename );
98
- provider->load_from_path (css_file);
99
- Log::info (" Using css file \' " , css_file, " \' " );
100
- }
101
- auto icon_theme = Gtk::IconTheme::get_for_screen (screen);
102
- if (!icon_theme) {
103
- Log::error (" Failed to load icon theme" );
104
- return EXIT_FAILURE;
105
- }
106
- auto & icon_theme_ref = *icon_theme.get ();
107
- auto icon_missing = Gdk::Pixbuf::create_from_file (DATA_DIR_STR " /nwgbar/icon-missing.svg" );
108
-
109
- BarWindow window{ config };
110
- window.set_background_color (background_color);
111
-
112
- /* Create buttons */
113
- for (auto & entry : bar_entries) {
114
- Gtk::Image* image = app_image (icon_theme_ref, entry.icon , icon_missing, config.icon_size );
115
- auto & ab = window.boxes .emplace_back (std::move (entry.name ),
116
- std::move (entry.exec ),
117
- std::move (entry.icon ));
118
- ab.set_image_position (Gtk::POS_TOP);
119
- ab.set_image (*image);
120
- }
89
+ std::vector<BarEntry> bar_entries {};
90
+ if (bar_json.size () > 0 ) {
91
+ bar_entries = get_bar_entries (std::move (bar_json));
92
+ }
121
93
122
- int column = 0 ;
123
- int row = 0 ;
94
+ Gtk::StyleContext::add_provider_for_screen (screen, provider, GTK_STYLE_PROVIDER_PRIORITY_USER);
95
+ {
96
+ auto css_file = setup_css_file (" nwgbar" , config_dir, config.css_filename );
97
+ provider->load_from_path (css_file);
98
+ Log::info (" Using css file \' " , css_file, " \' " );
99
+ }
100
+ IconProvider icon_provider {
101
+ Gtk::IconTheme::get_for_screen (screen),
102
+ config.icon_size
103
+ };
104
+
105
+ BarWindow window{ config };
106
+ window.set_background_color (background_color);
107
+
108
+ /* Create buttons */
109
+ for (auto & entry : bar_entries) {
110
+ auto image = Gtk::make_managed<Gtk::Image>(icon_provider.load_icon (entry.icon ));
111
+ auto & ab = window.boxes .emplace_back (std::move (entry.name ),
112
+ std::move (entry.exec ),
113
+ std::move (entry.icon ));
114
+ ab.set_image_position (Gtk::POS_TOP);
115
+ ab.set_image (*image);
116
+ }
124
117
125
- window.grid .freeze_child_notify ();
126
- for (auto & box : window.boxes ) {
127
- window.grid .attach (box, column, row, 1 , 1 );
128
- if (config.orientation == Orientation::Vertical) {
129
- row++;
130
- } else {
131
- column++;
118
+ int column = 0 ;
119
+ int row = 0 ;
120
+
121
+ window.grid .freeze_child_notify ();
122
+ for (auto & box : window.boxes ) {
123
+ window.grid .attach (box, column, row, 1 , 1 );
124
+ if (config.orientation == Orientation::Vertical) {
125
+ row++;
126
+ } else {
127
+ column++;
128
+ }
132
129
}
133
- }
134
- window.grid .thaw_child_notify ();
135
- window.show_all_children ();
136
- window.show (hint::Fullscreen);
130
+ window.grid .thaw_child_notify ();
131
+ Instance instance{ *app.get (), " nwgbar" };
132
+
133
+ window.show_all_children ();
134
+ window.show (hint::Fullscreen);
137
135
138
- gettimeofday (&tp, NULL );
139
- long int end_ms = tp.tv_sec * 1000 + tp.tv_usec / 1000 ;
136
+ gettimeofday (&tp, NULL );
137
+ long int end_ms = tp.tv_sec * 1000 + tp.tv_usec / 1000 ;
140
138
141
- Log::info (" Time: " , end_ms - start_ms, " ms" );
139
+ Log::info (" Time: " , end_ms - start_ms, " ms" );
142
140
143
- return app->run (window);
141
+ return app->run (window);
142
+ } catch (const Glib::FileError& error) {
143
+ Log::error (error.what ());
144
+ } catch (const std::runtime_error& error) {
145
+ Log::error (error.what ());
146
+ }
147
+ return EXIT_FAILURE;
144
148
}
0 commit comments