-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgui.cpp
More file actions
234 lines (195 loc) · 8.22 KB
/
Copy pathgui.cpp
File metadata and controls
234 lines (195 loc) · 8.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
* Copyright 2025 Toni500git
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
* following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
* disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#if GUI_APP
#define STB_IMAGE_IMPLEMENTATION
#include "gui.hpp"
#include <gdkmm/pixbufanimation.h>
#include <glibmm/main.h>
#include <glibmm/refptr.h>
#include <gtkmm/cssprovider.h>
#include <gtkmm/enums.h>
#include <unistd.h>
#include <array>
#include <filesystem>
#include <fstream>
#include "display.hpp"
#include "fmt/ranges.h"
#include "libs/stb_image.h"
#include "util.hpp"
using namespace GUI;
// https://www.codespeedy.com/convert-rgb-to-hex-color-code-in-cpp/
/*static std::string rgba_to_hexstr(const Gdk::RGBA& color) {
int red = color.get_red() * 255;
int green = color.get_green() * 255;
int blue = color.get_blue() * 255;
std::stringstream ss;
ss << "#" << std::hex << (red << 16 | green << 8 | blue);
return ss.str();
}*/
// Display::render but only for images on GUI
static std::vector<std::string> render_with_image(const Config& config)
{
std::string path{ Display::detect_distro(config) };
moduleMap_t modulesInfo{};
std::vector<std::string> layout{ config.args_layout.empty() ? config.layout : config.args_layout };
int image_width, image_height, channels;
// load the image and get its width and height
unsigned char* img = stbi_load(config.source_path.c_str(), &image_width, &image_height, &channels, 0);
if (!img)
die(_("Unable to load image '{}'"), config.source_path);
stbi_image_free(img);
if (!config.ascii_logo_type.empty())
{
const size_t& pos = path.rfind('.');
if (pos != std::string::npos)
path.insert(pos, "_" + config.ascii_logo_type);
else
path += "_" + config.ascii_logo_type;
}
// this is just for parse() to auto add the distro colors
std::ifstream file(path, std::ios::binary);
std::string line, _;
std::vector<std::string> tmp_layout;
parse_args_t parse_args{ modulesInfo, config, _, layout, tmp_layout, false };
while (std::getline(file, line))
{
parse(line, parse_args);
parse_args.no_more_reset = false;
}
parse_args.parsing_layout = true;
for (size_t i = 0; i < layout.size(); ++i)
{
layout[i] = parse(layout[i], parse_args);
parse_args.no_more_reset = false;
if (!tmp_layout.empty())
{
layout.erase(layout.begin() + i);
layout.insert(layout.begin() + i, tmp_layout.begin(), tmp_layout.end());
i += tmp_layout.size() - 1;
tmp_layout.clear();
}
}
// erase each element for each instance of MAGIC_LINE
layout.erase(std::remove_if(layout.begin(), layout.end(),
[](const std::string_view str) { return str.find(MAGIC_LINE) != std::string::npos; }),
layout.end());
const unsigned int offset =
(config.offset.back() == '%')
? Display::calc_perc(std::stof(config.offset.substr(0, config.offset.size() - 1)), image_width, 0)
: std::stoi(config.offset);
for (size_t i = 0; i < layout.size(); i++)
for (size_t _ = 0; _ < offset; _++) // I use _ because we don't need it
layout.at(i).insert(0, " ");
return layout;
}
bool Window::set_layout_markup()
{
if (m_isImage)
{
if (!m_config.args_print_logo_only)
m_label.set_markup(fmt::format("{}", fmt::join(render_with_image(m_config), "\n")));
}
else
{
m_label.set_markup(fmt::format("{}", fmt::join(Display::render(m_config, true, m_path, m_moduleMap), "\n")));
}
return true;
}
Window::Window(const Config& config, const std::filesystem::path& path, const moduleMap_t& moduleMap)
: m_config(config), m_path(path), m_moduleMap(moduleMap), m_isImage(false)
{
set_title("customfetch - Higly customizable and fast neofetch like program");
set_default_size(1000, 600);
set_position(Gtk::WIN_POS_CENTER_ALWAYS);
set_icon_from_file(ICONPREFIX "/customfetch/Thumbnail.png");
debug("Window::Window analyzing file");
std::ifstream f(path.c_str());
std::array<unsigned char, 32> buffer;
f.read(reinterpret_cast<char*>(&buffer.at(0)), buffer.size());
if (is_file_image(buffer.data()))
m_isImage = true;
// useImage can be either a gif or an image
if (m_isImage && !config.args_disable_source)
{
const auto& img = Gdk::PixbufAnimation::create_from_file(path.c_str());
m_img = Gtk::manage(new Gtk::Image(img));
m_img->set(img);
m_img->set_alignment(Gtk::ALIGN_CENTER);
m_box.pack_start(*m_img, Gtk::PACK_SHRINK);
}
m_box.set_orientation(Gtk::ORIENTATION_HORIZONTAL);
this->set_layout_markup();
if (is_live_mode)
Glib::signal_timeout().connect(sigc::mem_fun(*this, &Window::set_layout_markup), config.loop_ms);
if (config.gui_bg_image != "disable")
{
if (!std::filesystem::exists(config.gui_bg_image))
die(_("Background image path '{}' doesn't exist"), config.gui_bg_image);
m_bg_animation = Gdk::PixbufAnimation::create_from_file(config.gui_bg_image);
if (m_bg_animation->is_static_image())
{
// set it initially and resize only on window resize
m_bg_static_image = m_bg_animation->get_static_image();
update_static_image();
}
else
{
// animated image: use timer to update frames
const int duration = m_bg_animation->get_iter(nullptr)->get_delay_time();
Glib::signal_timeout().connect(sigc::mem_fun(*this, &Window::on_update_animation), duration);
}
m_overlay.add_overlay(m_bg_image);
}
if (config.gui_css_file != "disable")
{
if (!std::filesystem::exists(config.gui_css_file))
die(_("Path to gtk css file '{}' doesn't exist"), config.gui_css_file);
Glib::RefPtr<Gtk::CssProvider> css_provider = Gtk::CssProvider::create();
Glib::RefPtr<Gdk::Screen> screen = Gdk::Screen::get_default();
try
{
css_provider->load_from_path(config.gui_css_file);
}
catch (const Glib::Error& ex)
{
die(_("Failed to load CSS: {}"), ex.gobj()->message);
}
m_box.get_style_context()->add_provider_for_screen(screen, css_provider, GTK_STYLE_PROVIDER_PRIORITY_USER);
}
if (Display::ascii_logo_fd != -1)
{
::remove(path.c_str());
::close(Display::ascii_logo_fd);
}
m_box.pack_start(m_label, Gtk::PACK_SHRINK);
m_alignment.set(0.5, 0.5, 0, 0);
m_alignment.add(m_box);
m_overlay.add_overlay(m_alignment);
add(m_overlay);
signal_size_allocate().connect(sigc::mem_fun(*this, &Window::on_window_resize));
show_all_children();
}
Window::~Window() {}
#endif // GUI_APP