Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gtk4 #3857

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open

Gtk4 #3857

Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
GTK4 migration
Signed-off-by: Viktar Lukashonak <myxabeer@gmail.com>
  • Loading branch information
LukashonakV authored and tmccombs committed Jan 29, 2025
commit 1f315bd0f7414880b7a4a6a50494121ced598e35
6 changes: 4 additions & 2 deletions include/AAppIconLabel.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once

#include <gtkmm/box.h>
#include <gtkmm/image.h>
#include <gtkmm/icontheme.h>

#include "AIconLabel.hpp"

Expand All @@ -19,9 +18,12 @@ class AAppIconLabel : public AIconLabel {
void updateAppIconName(const std::string &app_identifier,
const std::string &alternative_app_identifier);
void updateAppIcon();

private:
unsigned app_icon_size_{24};
bool update_app_icon_{true};
std::string app_icon_name_;
Glib::RefPtr<const Gtk::IconTheme> gtkTheme_;
};

} // namespace waybar
1 change: 1 addition & 0 deletions include/AIconLabel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class AIconLabel : public ALabel {
bool enable_click = false, bool enable_scroll = false);
virtual ~AIconLabel() = default;
auto update() -> void override;
Gtk::Widget &root() override;

protected:
Gtk::Image image_;
Expand Down
18 changes: 8 additions & 10 deletions include/ALabel.hpp
Original file line number Diff line number Diff line change
@@ -1,36 +1,34 @@
#pragma once

#include <glibmm/markup.h>
#include <gtkmm/label.h>
#include <json/json.h>

#include <chrono>

#include "AModule.hpp"

namespace waybar {

class ALabel : public AModule {
public:
ALabel(const Json::Value &, const std::string &, const std::string &, const std::string &format,
uint16_t interval = 0, bool ellipsize = false, bool enable_click = false,
bool enable_scroll = false);
virtual ~ALabel() = default;
auto update() -> void override;
virtual std::string getIcon(uint16_t, const std::string &alt = "", uint16_t max = 0);
virtual std::string getIcon(uint16_t, const std::vector<std::string> &alts, uint16_t max = 0);
Gtk::Widget &root() override;

protected:
ALabel(const Json::Value &, const std::string &, const std::string &, const std::string &format,
uint16_t interval = 0, bool ellipsize = false, bool enable_click = false,
bool enable_scroll = false);

Gtk::Label label_;
std::string format_;
const std::chrono::seconds interval_;
bool alt_ = false;
std::string default_format_;

bool handleToggle(GdkEventButton *const &e) override;
void handleToggle(int n_press, double dx, double dy) override;
virtual std::string getState(uint8_t value, bool lesser = false);

std::map<std::string, GtkMenuItem *> submenus_;
std::map<std::string, std::string> menuActionsMap_;
static void handleGtkMenuEvent(GtkMenuItem *menuitem, gpointer data);
};

} // namespace waybar
107 changes: 69 additions & 38 deletions include/AModule.hpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,36 @@
#pragma once

#include <glibmm/dispatcher.h>
#include <glibmm/markup.h>
#include <gtkmm.h>
#include <gtkmm/eventbox.h>
#include <gtkmm/eventcontrollermotion.h>
#include <gtkmm/eventcontrollerscroll.h>
#include <gtkmm/gestureclick.h>
#include <json/json.h>

#include "IModule.hpp"

namespace waybar {

// Representation of a specific type of mouse event
struct MouseEvent {
// Number of button pressed
uint n_button;
// Number of times the button was pressed
int n_press;
// Whether it was a press or release
Gdk::Event::Type evt_type;

bool operator<(const MouseEvent o) const {
return std::tie(n_button, n_press, evt_type) < std::tie(o.n_button, o.n_press, o.evt_type);
}
};

class AModule : public IModule {
public:
static constexpr const char *MODULE_CLASS = "module";

~AModule() override;
auto update() -> void override;
virtual auto refresh(int shouldRefresh) -> void {};
operator Gtk::Widget &() override;
auto doAction(const std::string &name) -> void override;

/// Emitting on this dispatcher triggers a update() call
Expand All @@ -33,52 +46,70 @@ class AModule : public IModule {

enum SCROLL_DIR { NONE, UP, DOWN, LEFT, RIGHT };

SCROLL_DIR getScrollDir(GdkEventScroll *e);
const SCROLL_DIR getScrollDir(Glib::RefPtr<const Gdk::Event> e);
bool tooltipEnabled() const;

const std::string name_;
const Json::Value &config_;
Gtk::EventBox event_box_;
Glib::RefPtr<Gtk::GestureClick> controllClick_;
Glib::RefPtr<Gtk::EventControllerScroll> controllScroll_;
Glib::RefPtr<Gtk::EventControllerMotion> controllMotion_;

void bindEvents(Gtk::Widget &wg);
void unBindEvents();

virtual void setCursor(Gdk::CursorType const &c);
virtual void setCursor(const Glib::RefPtr<Gdk::Cursor> &cur);
virtual void setCursor(const Glib::ustring &name);

virtual bool handleToggle(GdkEventButton *const &ev);
virtual bool handleMouseEnter(GdkEventCrossing *const &ev);
virtual bool handleMouseLeave(GdkEventCrossing *const &ev);
virtual bool handleScroll(GdkEventScroll *);
virtual bool handleRelease(GdkEventButton *const &ev);
GObject *menu_;
virtual void handleToggle(int n_press, double dx, double dy);
virtual void handleRelease(int n_press, double dx, double dy);
virtual bool handleScroll(double dx, double dy);
virtual void handleMouseEnter(double x, double y);
virtual void handleMouseLeave();

private:
bool handleUserEvent(GdkEventButton *const &ev);
const bool isTooltip;
const bool isExpand;
bool hasUserEvents_;
const bool isAfter{true};
bool enableClick_{false};
bool enableScroll_{false};
bool hasPressEvents_{false};
bool hasReleaseEvents_{false};
std::vector<int> pid_;
gdouble distance_scrolled_y_;
gdouble distance_scrolled_x_;
gdouble distance_scrolled_y_{0.0};
gdouble distance_scrolled_x_{0.0};
const Glib::RefPtr<Gdk::Cursor> curDefault;
const Glib::RefPtr<Gdk::Cursor> curPoint;
Glib::RefPtr<const Gdk::Event> currEvent_;
std::map<std::string, std::string> eventActionMap_;
static const inline std::map<std::pair<uint, GdkEventType>, std::string> eventMap_{
{std::make_pair(1, GdkEventType::GDK_BUTTON_PRESS), "on-click"},
{std::make_pair(1, GdkEventType::GDK_BUTTON_RELEASE), "on-click-release"},
{std::make_pair(1, GdkEventType::GDK_2BUTTON_PRESS), "on-double-click"},
{std::make_pair(1, GdkEventType::GDK_3BUTTON_PRESS), "on-triple-click"},
{std::make_pair(2, GdkEventType::GDK_BUTTON_PRESS), "on-click-middle"},
{std::make_pair(2, GdkEventType::GDK_BUTTON_RELEASE), "on-click-middle-release"},
{std::make_pair(2, GdkEventType::GDK_2BUTTON_PRESS), "on-double-click-middle"},
{std::make_pair(2, GdkEventType::GDK_3BUTTON_PRESS), "on-triple-click-middle"},
{std::make_pair(3, GdkEventType::GDK_BUTTON_PRESS), "on-click-right"},
{std::make_pair(3, GdkEventType::GDK_BUTTON_RELEASE), "on-click-right-release"},
{std::make_pair(3, GdkEventType::GDK_2BUTTON_PRESS), "on-double-click-right"},
{std::make_pair(3, GdkEventType::GDK_3BUTTON_PRESS), "on-triple-click-right"},
{std::make_pair(8, GdkEventType::GDK_BUTTON_PRESS), "on-click-backward"},
{std::make_pair(8, GdkEventType::GDK_BUTTON_RELEASE), "on-click-backward-release"},
{std::make_pair(8, GdkEventType::GDK_2BUTTON_PRESS), "on-double-click-backward"},
{std::make_pair(8, GdkEventType::GDK_3BUTTON_PRESS), "on-triple-click-backward"},
{std::make_pair(9, GdkEventType::GDK_BUTTON_PRESS), "on-click-forward"},
{std::make_pair(9, GdkEventType::GDK_BUTTON_RELEASE), "on-click-forward-release"},
{std::make_pair(9, GdkEventType::GDK_2BUTTON_PRESS), "on-double-click-forward"},
{std::make_pair(9, GdkEventType::GDK_3BUTTON_PRESS), "on-triple-click-forward"}};
static const inline std::map<MouseEvent, std::string> eventMap_{
{{1u, 1, Gdk::Event::Type::BUTTON_PRESS}, "on-click"},
{{1u, 1, Gdk::Event::Type::BUTTON_RELEASE}, "on-click-release"},
{{1u, 2, Gdk::Event::Type::BUTTON_PRESS}, "on-double-click"},
{{1u, 3, Gdk::Event::Type::BUTTON_PRESS}, "on-triple-click"},
{{2u, 1, Gdk::Event::Type::BUTTON_PRESS}, "on-click-middle"},
{{2u, 1, Gdk::Event::Type::BUTTON_RELEASE}, "on-click-middle-release"},
{{2u, 2, Gdk::Event::Type::BUTTON_PRESS}, "on-double-click-middle"},
{{2u, 3, Gdk::Event::Type::BUTTON_PRESS}, "on-triple-click-middle"},
{{3u, 1, Gdk::Event::Type::BUTTON_PRESS}, "on-click-right"},
{{3u, 1, Gdk::Event::Type::BUTTON_RELEASE}, "on-click-right-release"},
{{3u, 2, Gdk::Event::Type::BUTTON_PRESS}, "on-double-click-right"},
{{3u, 3, Gdk::Event::Type::BUTTON_PRESS}, "on-triple-click-right"},
{{8u, 1, Gdk::Event::Type::BUTTON_PRESS}, "on-click-backward"},
{{8u, 1, Gdk::Event::Type::BUTTON_RELEASE}, "on-click-backward-release"},
{{8u, 2, Gdk::Event::Type::BUTTON_PRESS}, "on-double-click-backward"},
{{8u, 3, Gdk::Event::Type::BUTTON_PRESS}, "on-triple-click-backward"},
{{9u, 1, Gdk::Event::Type::BUTTON_PRESS}, "on-click-forward"},
{{9u, 1, Gdk::Event::Type::BUTTON_RELEASE}, "on-click-forward-release"},
{{9u, 2, Gdk::Event::Type::BUTTON_PRESS}, "on-double-click-forward"},
{{9u, 3, Gdk::Event::Type::BUTTON_PRESS}, "on-triple-click-forward"}};
void handleClickEvent(uint n_button, int n_press, Gdk::Event::Type n_evtype);
void makeControllClick();
void makeControllScroll();
void makeControllMotion();
void removeControllClick();
void removeControllScroll();
void removeControllMotion();
};

} // namespace waybar
3 changes: 2 additions & 1 deletion include/ASlider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ class ASlider : public AModule {
public:
ASlider(const Json::Value& config, const std::string& name, const std::string& id);
virtual void onValueChanged();
Gtk::Widget& root() override;

protected:
bool vertical_ = false;
int min_ = 0, max_ = 100, curr_ = 50;
Gtk::Scale scale_;
};

} // namespace waybar
} // namespace waybar
5 changes: 4 additions & 1 deletion include/IModule.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ class IModule {
public:
virtual ~IModule() = default;
virtual auto update() -> void = 0;
virtual operator Gtk::Widget&() = 0;
// Get the root widget of the module
virtual Gtk::Widget& root() = 0;
virtual auto doAction(const std::string& name) -> void = 0;

operator Gtk::Widget&() { return this->root(); }
};

} // namespace waybar
23 changes: 11 additions & 12 deletions include/bar.hpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
#pragma once

#include <gdkmm/monitor.h>
#include <glibmm/refptr.h>
#include <gtkmm/box.h>
#include <gtkmm/centerbox.h>
#include <gtkmm/cssprovider.h>
#include <gtkmm/main.h>
#include <gtkmm/window.h>
#include <json/json.h>

#include <memory>
#include <optional>
#include <vector>

#include "AModule.hpp"
#include "group.hpp"
Expand Down Expand Up @@ -75,8 +69,8 @@ class Bar : public sigc::trackable {
struct wl_surface *surface;
bool visible = true;
Gtk::Window window;
Gtk::Orientation orientation = Gtk::ORIENTATION_HORIZONTAL;
Gtk::PositionType position = Gtk::POS_TOP;
Gtk::Orientation orientation = Gtk::Orientation::HORIZONTAL;
Gtk::PositionType position = Gtk::PositionType::TOP;

int x_global;
int y_global;
Expand All @@ -86,18 +80,23 @@ class Bar : public sigc::trackable {
#endif

private:
void onMap(GdkEventAny *);
void onMap();
auto setupWidgets() -> void;
void getModules(const Factory &, const std::string &, waybar::Group *);
void setupAltFormatKeyForModule(const std::string &module_name);
void setupAltFormatKeyForModuleList(const char *module_list_name);
void setMode(const bar_mode &);
void setPassThrough(bool passthrough);
void setPosition(Gtk::PositionType position);
void onConfigure(GdkEventConfigure *ev);
void onConfigure(int width, int height);
void configureGlobalOffset(int width, int height);
void onOutputGeometryChanged();

void setExpand(Gtk::Widget &);
void addModulesToBox(Gtk::Box &, std::vector<std::shared_ptr<waybar::AModule>> &);

Glib::RefPtr<Gdk::Surface> gdk_surface_;

/* Copy initial set of modes to allow customization */
bar_mode_map configured_modes = PRESET_MODES;
std::string last_mode_{MODE_DEFAULT};
Expand All @@ -109,7 +108,7 @@ class Bar : public sigc::trackable {
Gtk::Box left_;
Gtk::Box center_;
Gtk::Box right_;
Gtk::Box box_;
Gtk::CenterBox box_;
std::vector<std::shared_ptr<waybar::AModule>> modules_left_;
std::vector<std::shared_ptr<waybar::AModule>> modules_center_;
std::vector<std::shared_ptr<waybar::AModule>> modules_right_;
Expand Down
7 changes: 2 additions & 5 deletions include/client.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#pragma once

#include <fmt/format.h>
#include <gdk/gdk.h>
#include <gdk/gdkwayland.h>
#include <wayland-client.h>
#include <gdk/wayland/gdkwayland.h>

#include "bar.hpp"
#include "config.hpp"
Expand Down Expand Up @@ -33,6 +30,7 @@ class Client {

private:
Client() = default;
Glib::RefPtr<Gio::ListModel> monitors_;
const std::string getStyle(const std::string &style, std::optional<Appearance> appearance);
void bindInterfaces();
void handleOutput(struct waybar_output &output);
Expand All @@ -50,7 +48,6 @@ class Client {
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_;
Expand Down
2 changes: 0 additions & 2 deletions include/factory.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#pragma once

#include <json/json.h>

#include <AModule.hpp>

namespace waybar {
Expand Down
32 changes: 16 additions & 16 deletions include/group.hpp
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
#pragma once

#include <gtkmm/box.h>
#include <gtkmm/widget.h>
#include <json/json.h>
#include <gtkmm/revealer.h>

#include "AModule.hpp"
#include "gtkmm/revealer.h"

namespace waybar {

class Group : public AModule {
class Group final : public AModule {
public:
Group(const std::string &, const std::string &, const Json::Value &, bool);
~Group() override = default;
auto update() -> void override;
operator Gtk::Widget &() override;

virtual Gtk::Box &getBox();
void addWidget(Gtk::Widget &widget);

protected:
Gtk::Box box;
Gtk::Box revealer_box;
Gtk::Revealer revealer;
bool is_first_widget = true;
bool is_drawer = false;
bool click_to_reveal = false;
std::string add_class_to_drawer_children;
bool handleMouseEnter(GdkEventCrossing *const &ev) override;
bool handleMouseLeave(GdkEventCrossing *const &ev) override;
bool handleToggle(GdkEventButton *const &ev) override;
Gtk::Widget &root() override;

private:
Gtk::Box box_;
Gtk::Box revealer_box_;
Gtk::Revealer revealer_;
bool is_first_widget_ = true;
bool is_drawer_ = false;
bool click_to_reveal_ = false;
std::string add_class_to_drawer_children_;

void handleMouseEnter(double x, double y) override;
void handleMouseLeave() override;
void handleToggle(int n_press, double dx, double dy) override;
void show_group();
void hide_group();
};
Expand Down
Loading