Merge pull request #4804 from BBaoVanC/fix-appearance-change-css-watching
fix: Watch new CSS files on appearance change
This commit is contained in:
@@ -14,12 +14,14 @@ struct pollfd;
|
|||||||
namespace waybar {
|
namespace waybar {
|
||||||
class CssReloadHelper {
|
class CssReloadHelper {
|
||||||
public:
|
public:
|
||||||
CssReloadHelper(std::string cssFile, std::function<void()> callback);
|
CssReloadHelper(std::string cssFile, std::function<void(const std::string&)> callback);
|
||||||
|
|
||||||
virtual ~CssReloadHelper() = default;
|
virtual ~CssReloadHelper() = default;
|
||||||
|
|
||||||
virtual void monitorChanges();
|
virtual void monitorChanges();
|
||||||
|
|
||||||
|
virtual void changeCssFile(const std::string& newCssFile);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::vector<std::string> parseImports(const std::string& cssFile);
|
std::vector<std::string> parseImports(const std::string& cssFile);
|
||||||
|
|
||||||
@@ -42,7 +44,7 @@ class CssReloadHelper {
|
|||||||
private:
|
private:
|
||||||
std::string m_cssFile;
|
std::string m_cssFile;
|
||||||
|
|
||||||
std::function<void()> m_callback;
|
std::function<void(const std::string&)> m_callback;
|
||||||
|
|
||||||
std::vector<std::tuple<Glib::RefPtr<Gio::FileMonitor>>> m_fileMonitors;
|
std::vector<std::tuple<Glib::RefPtr<Gio::FileMonitor>>> m_fileMonitors;
|
||||||
};
|
};
|
||||||
|
|||||||
+2
-1
@@ -306,9 +306,10 @@ int waybar::Client::main(int argc, char* argv[]) {
|
|||||||
}
|
}
|
||||||
m_cssFile = getStyle(style_opt);
|
m_cssFile = getStyle(style_opt);
|
||||||
setupCss(m_cssFile);
|
setupCss(m_cssFile);
|
||||||
m_cssReloadHelper = std::make_unique<CssReloadHelper>(m_cssFile, [&]() { setupCss(m_cssFile); });
|
m_cssReloadHelper = std::make_unique<CssReloadHelper>(m_cssFile, [&](const std::string& css_file) { setupCss(css_file); });
|
||||||
portal->signal_appearance_changed().connect([&](waybar::Appearance appearance) {
|
portal->signal_appearance_changed().connect([&](waybar::Appearance appearance) {
|
||||||
auto css_file = getStyle(style_opt, appearance);
|
auto css_file = getStyle(style_opt, appearance);
|
||||||
|
m_cssReloadHelper->changeCssFile(css_file);
|
||||||
setupCss(css_file);
|
setupCss(css_file);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -23,7 +23,8 @@ namespace {
|
|||||||
const std::regex IMPORT_REGEX(R"(@import\s+(?:url\()?(?:"|')([^"')]+)(?:"|')\)?;)");
|
const std::regex IMPORT_REGEX(R"(@import\s+(?:url\()?(?:"|')([^"')]+)(?:"|')\)?;)");
|
||||||
}
|
}
|
||||||
|
|
||||||
waybar::CssReloadHelper::CssReloadHelper(std::string cssFile, std::function<void()> callback)
|
waybar::CssReloadHelper::CssReloadHelper(std::string cssFile,
|
||||||
|
std::function<void(const std::string&)> callback)
|
||||||
: m_cssFile(std::move(cssFile)), m_callback(std::move(callback)) {}
|
: m_cssFile(std::move(cssFile)), m_callback(std::move(callback)) {}
|
||||||
|
|
||||||
std::string waybar::CssReloadHelper::getFileContents(const std::string& filename) {
|
std::string waybar::CssReloadHelper::getFileContents(const std::string& filename) {
|
||||||
@@ -88,6 +89,12 @@ void waybar::CssReloadHelper::monitorChanges() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void waybar::CssReloadHelper::changeCssFile(const std::string& newCssFile) {
|
||||||
|
m_fileMonitors.clear();
|
||||||
|
m_cssFile = newCssFile;
|
||||||
|
monitorChanges();
|
||||||
|
}
|
||||||
|
|
||||||
void waybar::CssReloadHelper::handleFileChange(Glib::RefPtr<Gio::File> const& file,
|
void waybar::CssReloadHelper::handleFileChange(Glib::RefPtr<Gio::File> const& file,
|
||||||
Glib::RefPtr<Gio::File> const& other_type,
|
Glib::RefPtr<Gio::File> const& other_type,
|
||||||
Gio::FileMonitorEvent event_type) {
|
Gio::FileMonitorEvent event_type) {
|
||||||
@@ -95,7 +102,7 @@ void waybar::CssReloadHelper::handleFileChange(Glib::RefPtr<Gio::File> const& fi
|
|||||||
// fire for one
|
// fire for one
|
||||||
if (event_type == Gio::FileMonitorEvent::FILE_MONITOR_EVENT_CHANGES_DONE_HINT) {
|
if (event_type == Gio::FileMonitorEvent::FILE_MONITOR_EVENT_CHANGES_DONE_HINT) {
|
||||||
spdlog::debug("Reloading style, file changed: {}", file->get_path());
|
spdlog::debug("Reloading style, file changed: {}", file->get_path());
|
||||||
m_callback();
|
m_callback(m_cssFile);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,7 @@
|
|||||||
|
|
||||||
class CssReloadHelperTest : public waybar::CssReloadHelper {
|
class CssReloadHelperTest : public waybar::CssReloadHelper {
|
||||||
public:
|
public:
|
||||||
CssReloadHelperTest() : CssReloadHelper("/tmp/waybar_test.css", [this]() { callback(); }) {}
|
CssReloadHelperTest() : CssReloadHelper("/tmp/waybar_test.css", [this](const std::string&) { callback(); }) {}
|
||||||
|
|
||||||
void callback() { m_callbackCounter++; }
|
void callback() { m_callbackCounter++; }
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user