mirror of
https://github.com/rad4day/Waybar.git
synced 2025-07-13 22:52:30 +02:00
search for dark or light mode stylesheet
summary: ------- This commit adds xdg-desktop-portal support to waybar. If a portal supporting `org.freedesktop.portal.Settings` exists, then it will be queried for the current colorscheme. This colorscheme will then be used to prefer a `style-light.css` or `style-dark.css` over the basic `style.css`. technical details: ----------------- Appearance is provided by several libraries, such as libhandy (mobile) and libadwaita. However, waybar links to neither of these libraries. As the amount of code required to communicate with xdg-desktop portal as a client is rather minimal, I believe doing so is better than linking to an additional library. The Gio library for communicating with dbus is rather messy, Instead of the `Portal` class containing a `Gio::Dbus::Proxy`, it extends it which simplifies signal handling. `Portal` then exposes its own signal, which can be listened to by waybar to update CSS. For a reference implementation, please see another one of my projects: https://github.com/4e554c4c/darkman.nvim/blob/main/portal.go test plan: --------- If no desktop portal which provides `Settings` exists, then waybar continues with the log line ``` [2023-09-06 14:14:37.754] [info] Unable to receive desktop appearance: GDBus.Error:org.freedesktop.DBus.Error.UnknownMethod: No such interface “org.freedesktop.portal.Settings” on object at path /org/freedesktop/portal/desktop ``` Furthermore, if `style-light.css` or `style-dark.css` do not exist, then `style.css` will still be searched for. Waybar has been tested with both light and dark startup. E.g. if the appearance is dark on startup the log lines ``` [2023-09-06 14:27:45.379] [info] Discovered appearance 'dark' [2023-09-06 14:27:45.379] [debug] Try expanding: $XDG_CONFIG_HOME/waybar/style-dark.css [2023-09-06 14:27:45.379] [debug] Found config file: $XDG_CONFIG_HOME/waybar/style-dark.css [2023-09-06 14:27:45.379] [info] Using CSS file /home/pounce/.config/waybar/style-dark.css ``` will be observed. If the color then changes to light during the operation of waybar, it will change css files: ``` [2023-09-06 14:28:17.173] [info] Received new appearance 'dark' [2023-09-06 14:28:17.173] [debug] Try expanding: $XDG_CONFIG_HOME/waybar/style-light.css [2023-09-06 14:28:17.173] [debug] Found config file: $XDG_CONFIG_HOME/waybar/style-light.css [2023-09-06 14:28:17.173] [info] Using CSS file /home/pounce/.config/waybar/style-light.css ``` Finally, tested resetting waybar and toggling style (works, and style is only changed once). fixes: Alexays/Waybar#1973
This commit is contained in:
@ -7,6 +7,7 @@
|
||||
|
||||
#include "bar.hpp"
|
||||
#include "config.hpp"
|
||||
#include "util/portal.hpp"
|
||||
|
||||
struct zwlr_layer_shell_v1;
|
||||
struct zwp_idle_inhibitor_v1;
|
||||
@ -33,7 +34,7 @@ class Client {
|
||||
|
||||
private:
|
||||
Client() = default;
|
||||
const std::string getStyle(const std::string &style);
|
||||
const std::string getStyle(const std::string &style, std::optional<Appearance> appearance);
|
||||
void bindInterfaces();
|
||||
void handleOutput(struct waybar_output &output);
|
||||
auto setupCss(const std::string &css_file) -> void;
|
||||
@ -46,12 +47,14 @@ class Client {
|
||||
static void handleOutputDone(void *, struct zxdg_output_v1 *);
|
||||
static void handleOutputName(void *, struct zxdg_output_v1 *, const char *);
|
||||
static void handleOutputDescription(void *, struct zxdg_output_v1 *, const char *);
|
||||
void handleAppearanceChanged(waybar::Appearance appearance);
|
||||
void handleMonitorAdded(Glib::RefPtr<Gdk::Monitor> monitor);
|
||||
void handleMonitorRemoved(Glib::RefPtr<Gdk::Monitor> monitor);
|
||||
void handleDeferredMonitorRemoval(Glib::RefPtr<Gdk::Monitor> monitor);
|
||||
|
||||
Glib::RefPtr<Gtk::StyleContext> style_context_;
|
||||
Glib::RefPtr<Gtk::CssProvider> css_provider_;
|
||||
std::unique_ptr<Portal> portal;
|
||||
std::list<struct waybar_output> outputs_;
|
||||
};
|
||||
|
||||
|
38
include/util/portal.hpp
Normal file
38
include/util/portal.hpp
Normal file
@ -0,0 +1,38 @@
|
||||
#include <giomm/dbusproxy.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "fmt/format.h"
|
||||
|
||||
namespace waybar {
|
||||
|
||||
using namespace Gio;
|
||||
|
||||
enum class Appearance {
|
||||
UNKNOWN = 0,
|
||||
DARK = 1,
|
||||
LIGHT = 2,
|
||||
};
|
||||
class Portal : private DBus::Proxy {
|
||||
public:
|
||||
Portal();
|
||||
void refreshAppearance();
|
||||
Appearance getAppearance();
|
||||
|
||||
typedef sigc::signal<void, Appearance> type_signal_appearance_changed;
|
||||
type_signal_appearance_changed signal_appearance_changed() { return m_signal_appearance_changed; }
|
||||
|
||||
private:
|
||||
type_signal_appearance_changed m_signal_appearance_changed;
|
||||
Appearance currentMode;
|
||||
void on_signal(const Glib::ustring& sender_name, const Glib::ustring& signal_name,
|
||||
const Glib::VariantContainerBase& parameters);
|
||||
};
|
||||
|
||||
} // namespace waybar
|
||||
|
||||
template <>
|
||||
struct fmt::formatter<waybar::Appearance> : formatter<fmt::string_view> {
|
||||
// parse is inherited from formatter<string_view>.
|
||||
auto format(waybar::Appearance c, format_context& ctx) const;
|
||||
};
|
Reference in New Issue
Block a user