Skip to content

Commit 1e99599

Browse files
authored
Merge pull request #148 from Siborgium/master
Use fs::path instead of std::string plus some extra
2 parents 1821f2a + 6803b9b commit 1e99599

File tree

12 files changed

+160
-199
lines changed

12 files changed

+160
-199
lines changed

bar/bar.cc

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include "on_event.h"
1717
#include "bar.h"
1818

19-
RGBA background = {0.0, 0.0, 0.0, 0.9};
2019
std::string wm {""}; // detected or forced window manager name
2120
const char* const HELP_MESSAGE =
2221
"GTK button bar: nwgbar " VERSION_STR " (c) Piotr Miller & Contributors 2020\n\n\
@@ -88,24 +87,7 @@ int main(int argc, char *argv[]) {
8887
wm = wm_name;
8988
}
9089

91-
auto opa = input.getCmdOption("-o");
92-
if (!opa.empty()){
93-
try {
94-
auto o = std::stod(std::string{opa});
95-
if (o >= 0.0 && o <= 1.0) {
96-
background.alpha = o;
97-
} else {
98-
std::cerr << "\nERROR: Opacity must be in range 0.0 to 1.0\n\n";
99-
}
100-
} catch (...) {
101-
std::cerr << "\nERROR: Invalid opacity value\n\n";
102-
}
103-
}
104-
105-
std::string_view bcg = input.getCmdOption("-b");
106-
if (!bcg.empty()) {
107-
set_background(bcg);
108-
}
90+
auto background_color = input.get_background_color(0.9);
10991

11092
auto i_size = input.getCmdOption("-s");
11193
if (!i_size.empty()) {
@@ -124,16 +106,16 @@ int main(int argc, char *argv[]) {
124106
}
125107
}
126108

127-
std::string config_dir = get_config_dir("nwgbar");
109+
auto config_dir = get_config_dir("nwgbar");
128110
if (!fs::is_directory(config_dir)) {
129111
std::cout << "Config dir not found, creating...\n";
130112
fs::create_directories(config_dir);
131113
}
132114

133115
// default and custom style sheet
134-
std::string default_css_file = config_dir + "/style.css";
116+
auto default_css_file = config_dir / "style.css";
135117
// css file to be used
136-
std::string css_file = config_dir + "/" + custom_css_file;
118+
auto css_file = config_dir / custom_css_file;
137119
// copy default file if not found
138120
if (!fs::exists(default_css_file)) {
139121
try {
@@ -144,8 +126,8 @@ int main(int argc, char *argv[]) {
144126
}
145127

146128
// default or custom template
147-
std::string default_bar_file = config_dir + "/bar.json";
148-
std::string custom_bar_file = config_dir + "/" + definition_file;
129+
auto default_bar_file = config_dir / "bar.json";
130+
auto custom_bar_file = config_dir / definition_file;
149131
// copy default anyway if not found
150132
if (!fs::exists(default_bar_file)) {
151133
try {
@@ -155,12 +137,12 @@ int main(int argc, char *argv[]) {
155137
}
156138
}
157139

158-
ns::json bar_json {};
140+
ns::json bar_json;
159141
try {
160-
bar_json = get_bar_json(std::move(custom_bar_file));
142+
bar_json = json_from_file(custom_bar_file);
161143
} catch (...) {
162-
std::cerr << "\nERROR: Template file not found, using default\n";
163-
bar_json = get_bar_json(default_bar_file);
144+
std::cerr << "ERROR: Template file not found, using default\n";
145+
bar_json = json_from_file(default_bar_file);
164146
}
165147
std::cout << bar_json.size() << " bar entries loaded\n";
166148

@@ -210,6 +192,7 @@ int main(int argc, char *argv[]) {
210192
}
211193

212194
MainWindow window;
195+
window.set_background_color(background_color);
213196
window.show();
214197

215198
window.signal_button_press_event().connect(sigc::ptr_fun(&on_window_clicked));

bar/bar.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,4 @@ struct BarEntry {
6060
/*
6161
* Function declarations
6262
* */
63-
ns::json get_bar_json(const std::string&);
6463
std::vector<BarEntry> get_bar_entries(ns::json&&);

bar/bar_tools.cc

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,6 @@
99
#include "nwg_tools.h"
1010
#include "bar.h"
1111

12-
/*
13-
* Returns json object out of a template file
14-
* */
15-
ns::json get_bar_json(const std::string& custom_bar) {
16-
std::string bar_string = read_file_to_string(custom_bar);
17-
18-
return string_to_json(bar_string);
19-
}
20-
2112
/*
2213
* Returns a vector of BarEntry data structs
2314
* */

common/nwg_classes.cc

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <iostream>
1515

1616
#include "nwg_classes.h"
17+
#include "nwg_tools.h"
1718

1819
InputParser::InputParser (int argc, char **argv) {
1920
tokens.reserve(argc - 1);
@@ -35,6 +36,22 @@ bool InputParser::cmdOptionExists(std::string_view option) const {
3536
!= this->tokens.end();
3637
}
3738

39+
RGBA InputParser::get_background_color(double default_opacity) const {
40+
RGBA color{ 0.0, 0.0, 0.0, default_opacity };
41+
if (auto opacity_str = getCmdOption("-o"); !opacity_str.empty()) {
42+
auto opacity = std::stod(std::string{opacity_str});
43+
if (opacity >= 0.0 && opacity <= 1.0) {
44+
color.alpha = opacity;
45+
} else {
46+
std::cerr << "ERROR: Opacity must be in range 0.0 to 1.0\n";
47+
}
48+
}
49+
if (auto color_str = getCmdOption("-b"); !color_str.empty()) {
50+
decode_color(color_str, color);
51+
}
52+
return color;
53+
}
54+
3855
CommonWindow::CommonWindow(const Glib::ustring& title, const Glib::ustring& role) {
3956
set_title(title);
4057
set_role(role);
@@ -48,10 +65,11 @@ CommonWindow::~CommonWindow() { }
4865

4966
bool CommonWindow::on_draw(const Cairo::RefPtr<Cairo::Context>& cr) {
5067
cr->save();
68+
auto [r, g, b, a] = this->background_color;
5169
if (_SUPPORTS_ALPHA) {
52-
cr->set_source_rgba(background.red, background.green, background.blue, background.alpha);
70+
cr->set_source_rgba(r, g, b, a);
5371
} else {
54-
cr->set_source_rgb(background.red, background.green, background.blue);
72+
cr->set_source_rgb(r, g, b);
5573
}
5674
cr->set_operator(Cairo::OPERATOR_SOURCE);
5775
cr->paint();
@@ -75,6 +93,10 @@ void CommonWindow::check_screen() {
7593
gtk_widget_set_visual(GTK_WIDGET(gobj()), visual->gobj());
7694
}
7795

96+
void CommonWindow::set_background_color(RGBA color) {
97+
this->background_color = color;
98+
}
99+
78100
AppBox::AppBox() {
79101
this -> set_always_show_image(true);
80102
}

common/nwg_classes.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@
1717
#include <gtkmm.h>
1818
#include <glibmm/ustring.h>
1919

20+
struct RGBA {
21+
double red;
22+
double green;
23+
double blue;
24+
double alpha;
25+
};
26+
2027
/*
2128
* Argument parser
2229
* Credits for this cool class go to iain at https://stackoverflow.com/a/868894
@@ -28,7 +35,7 @@ class InputParser{
2835
std::string_view getCmdOption(std::string_view) const;
2936
/// @author iain
3037
bool cmdOptionExists(std::string_view) const;
31-
38+
RGBA get_background_color(double default_opacity) const;
3239
private:
3340
std::vector <std::string_view> tokens;
3441
};
@@ -39,10 +46,12 @@ class CommonWindow : public Gtk::Window {
3946
virtual ~CommonWindow();
4047

4148
void check_screen();
49+
void set_background_color(RGBA color);
4250
protected:
4351
bool on_draw(const ::Cairo::RefPtr< ::Cairo::Context>& cr) override;
4452
void on_screen_changed(const Glib::RefPtr<Gdk::Screen>& previous_screen) override;
4553
private:
54+
RGBA background_color;
4655
bool _SUPPORTS_ALPHA;
4756
};
4857

@@ -78,11 +87,3 @@ struct DesktopEntry {
7887
std::string mime_type;
7988
bool terminal;
8089
};
81-
82-
struct RGBA {
83-
double red;
84-
double green;
85-
double blue;
86-
double alpha;
87-
};
88-
extern RGBA background;

0 commit comments

Comments
 (0)