Skip to content

Commit

Permalink
start porting to gtk4
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeStanger committed Apr 22, 2023
1 parent 9d09855 commit f94939e
Show file tree
Hide file tree
Showing 20 changed files with 226 additions and 190 deletions.
246 changes: 142 additions & 104 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ workspaces = ["futures-util"]

[dependencies]
# core
gtk = "0.17.0"
gtk-layer-shell = "0.6.0"
glib = "0.17.5"
gtk = { package = "gtk4", version = "0.6.6" }
gtk-layer-shell = { package = "gtk4-layer-shell", version = "0.0.3" }
glib = "0.17.9"
tokio = { version = "1.21.2", features = ["macros", "rt-multi-thread", "time", "process", "sync", "io-util", "net"] }
tracing = "0.1.37"
tracing-subscriber = { version = "0.3.16", features = ["env-filter"] }
Expand Down
9 changes: 5 additions & 4 deletions src/bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use gtk::gdk::Monitor;
use gtk::prelude::*;
use gtk::{Application, ApplicationWindow, IconTheme, Orientation};
use std::sync::{Arc, RwLock};
use glib::signal::Inhibit;
use tracing::{debug, info};

/// Creates a new window for a bar,
Expand Down Expand Up @@ -48,16 +49,16 @@ pub fn create_bar(
let center = create_container("center", orientation);
let end = create_container("end", orientation);

content.add(&start);
content.append(&start);
content.set_center_widget(Some(&center));
content.pack_end(&end, false, false, 0);

load_modules(&start, &center, &end, app, config, monitor, monitor_name)?;
win.add(&content);
win.append(&content);

win.connect_destroy_event(|_, _| {
info!("Shutting down");
gtk::main_quit();
// gtk::main_quit();
Inhibit(false)
});

Expand Down Expand Up @@ -195,7 +196,7 @@ fn add_modules(
let common = $module.common.take().expect("Common config did not exist");
let widget = create_module(*$module, $id, &info, &Arc::clone(&popup))?;
let container = wrap_widget(&widget, common);
content.add(&container);
content.append(&container);
}};
}

Expand Down
29 changes: 15 additions & 14 deletions src/config/common.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use glib::signal::Inhibit;
use crate::dynamic_string::DynamicString;
use crate::script::{Script, ScriptInput};
use crate::send;
use gtk::gdk::ScrollDirection;
use gtk::prelude::*;
use gtk::EventBox;
use gtk::{GestureClick, Widget};
use serde::Deserialize;
use tokio::spawn;
use tracing::trace;
Expand All @@ -27,14 +28,16 @@ pub struct CommonConfig {

impl CommonConfig {
/// Configures the module's container according to the common config options.
pub fn install(mut self, container: &EventBox) {
self.install_show_if(container);
pub fn install<W: IsA<Widget>>(mut self, widget: &W) {
self.install_show_if(widget);

let left_click_script = self.on_click_left.map(Script::new_polling);
let middle_click_script = self.on_click_middle.map(Script::new_polling);
let right_click_script = self.on_click_right.map(Script::new_polling);

container.connect_button_press_event(move |_, event| {
let gesture = GestureClick::new();

gesture.connect_pressed(move |_, event| {
let script = match event.button() {
1 => left_click_script.as_ref(),
2 => middle_click_script.as_ref(),
Expand All @@ -46,14 +49,12 @@ impl CommonConfig {
trace!("Running on-click script: {}", event.button());
script.run_as_oneshot(None);
}

Inhibit(false)
});

let scroll_up_script = self.on_scroll_up.map(Script::new_polling);
let scroll_down_script = self.on_scroll_down.map(Script::new_polling);

container.connect_scroll_event(move |_, event| {
widget.connect_scroll_event(move |_, event| {
let script = match event.direction() {
ScrollDirection::Up => scroll_up_script.as_ref(),
ScrollDirection::Down => scroll_down_script.as_ref(),
Expand All @@ -71,7 +72,7 @@ impl CommonConfig {
macro_rules! install_oneshot {
($option:expr, $method:ident) => {
$option.map(Script::new_polling).map(|script| {
container.$method(move |_, _| {
widget.$method(move |_, _| {
script.run_as_oneshot(None);
Inhibit(false)
});
Expand All @@ -83,22 +84,22 @@ impl CommonConfig {
install_oneshot!(self.on_mouse_exit, connect_leave_notify_event);

if let Some(tooltip) = self.tooltip {
let container = container.clone();
let container = widget.clone();
DynamicString::new(&tooltip, move |string| {
container.set_tooltip_text(Some(&string));
Continue(true)
});
}
}

fn install_show_if(&mut self, container: &EventBox) {
fn install_show_if<W: IsA<Widget>>(&mut self, widget: &W) {
self.show_if.take().map_or_else(
|| {
container.show_all();
widget.set_visible(true)
},
|show_if| {
let script = Script::new_polling(show_if);
let container = container.clone();
let widget = widget.clone();
let (tx, rx) = glib::MainContext::channel(glib::PRIORITY_DEFAULT);
spawn(async move {
script
Expand All @@ -109,9 +110,9 @@ impl CommonConfig {
});
rx.attach(None, move |success| {
if success {
container.show_all();
widget.set_visible(true);
} else {
container.hide();
widget.hide();
};
Continue(true)
});
Expand Down
4 changes: 2 additions & 2 deletions src/image/gtk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub fn new_icon_label(input: &str, icon_theme: &IconTheme, size: i32) -> gtk::Bo
let image = Image::new();
image.set_widget_name("image");

container.add(&image);
container.append(&image);

if let Err(err) = ImageProvider::parse(input, icon_theme, size)
.and_then(|provider| provider.load_into_image(image))
Expand All @@ -49,7 +49,7 @@ pub fn new_icon_label(input: &str, icon_theme: &IconTheme, size: i32) -> gtk::Bo
let label = Label::new(Some(input));
label.set_widget_name("label");

container.add(&label);
container.append(&label);
}

container
Expand Down
7 changes: 5 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::style::load_css;
use color_eyre::eyre::Result;
use color_eyre::Report;
use dirs::config_dir;
use gtk::gdk::Display;
use gtk::gdk::{Display, Monitor};
use gtk::prelude::*;
use gtk::Application;
use std::env;
Expand Down Expand Up @@ -120,7 +120,10 @@ fn create_bars(
debug!("Received {} outputs from Wayland", outputs.len());
debug!("Outputs: {:?}", outputs);

let num_monitors = display.n_monitors();
for monitor in display.monitors().iter::<Monitor>() {
let monitor = monitor.unwrap();
}


for i in 0..num_monitors {
let monitor = display
Expand Down
17 changes: 8 additions & 9 deletions src/modules/clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ use crate::try_send;
use gtk::gdk_pixbuf::Pixbuf;
use gtk::gio::{Cancellable, MemoryInputStream};
use gtk::prelude::*;
use gtk::{Button, EventBox, Image, Label, Orientation, RadioButton, Widget};
use gtk::{Button, Image, Label, Orientation, RadioButton, Widget};
use serde::Deserialize;
use std::collections::HashMap;
use std::sync::Arc;
use glib::signal::Inhibit;
use tokio::spawn;
use tokio::sync::mpsc::{Receiver, Sender};
use tracing::{debug, error};
Expand Down Expand Up @@ -154,10 +155,10 @@ impl Module<Button> for ClipboardModule {
.build();

let entries = gtk::Box::new(Orientation::Vertical, 5);
container.add(&entries);
container.append(&entries);

let hidden_option = RadioButton::new();
entries.add(&hidden_option);
entries.append(&hidden_option);

let mut items = HashMap::new();

Expand All @@ -176,7 +177,7 @@ impl Module<Button> for ClipboardModule {
let button = RadioButton::from_widget(&hidden_option);

let label = Label::new(Some(value));
button.add(&label);
button.append(&label);

if let Some(truncate) = self.truncate {
truncate.truncate_label(&label);
Expand Down Expand Up @@ -211,7 +212,7 @@ impl Module<Button> for ClipboardModule {
button.set_active(true); // if just added, should be on clipboard

let button_wrapper = EventBox::new();
button_wrapper.add(&button);
button_wrapper.append(&button);

button_wrapper.set_widget_name(&format!("copy-{id}"));
button_wrapper.set_above_child(true);
Expand Down Expand Up @@ -254,12 +255,11 @@ impl Module<Button> for ClipboardModule {
});
}

row.add(&button_wrapper);
row.append(&button_wrapper);
row.pack_end(&remove_button, false, false, 0);

entries.add(&row);
entries.append(&row);
entries.reorder_child(&row, 0);
row.show_all();

items.insert(id, (row, button));
}
Expand Down Expand Up @@ -293,7 +293,6 @@ impl Module<Button> for ClipboardModule {
});
}

container.show_all();
hidden_option.hide();

Some(container)
Expand Down
8 changes: 3 additions & 5 deletions src/modules/clock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl Module<Button> for ClockModule {
let button = Button::new();
let label = Label::new(None);
label.set_angle(info.bar_position.get_angle());
button.add(&label);
button.append(&label);

let orientation = info.bar_position.get_orientation();
button.connect_clicked(move |button| {
Expand Down Expand Up @@ -107,10 +107,10 @@ impl Module<Button> for ClockModule {
.build();
let format = "%H:%M:%S";

container.add(&clock);
container.append(&clock);

let calendar = Calendar::builder().name("calendar").build();
container.add(&calendar);
container.append(&calendar);

{
rx.attach(None, move |date| {
Expand All @@ -120,8 +120,6 @@ impl Module<Button> for ClockModule {
});
}

container.show_all();

Some(container)
}
}
2 changes: 1 addition & 1 deletion src/modules/custom/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl CustomWidget for ButtonWidget {
if let Some(text) = self.label {
let label = Label::new(None);
label.set_use_markup(true);
button.add(&label);
button.append(&label);

DynamicString::new(&text, move |string| {
label.set_markup(&string);
Expand Down
14 changes: 6 additions & 8 deletions src/modules/custom/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,12 @@ impl Widget {
/// Creates this widget and adds it to the parent container
fn add_to(self, parent: &gtk::Box, context: CustomWidgetContext) {
match self {
Self::Box(widget) => parent.add(&widget.into_widget(context)),
Self::Label(widget) => parent.add(&widget.into_widget(context)),
Self::Button(widget) => parent.add(&widget.into_widget(context)),
Self::Image(widget) => parent.add(&widget.into_widget(context)),
Self::Slider(widget) => parent.add(&widget.into_widget(context)),
Self::Progress(widget) => parent.add(&widget.into_widget(context)),
Self::Box(widget) => parent.append(&widget.into_widget(context)),
Self::Label(widget) => parent.append(&widget.into_widget(context)),
Self::Button(widget) => parent.append(&widget.into_widget(context)),
Self::Image(widget) => parent.append(&widget.into_widget(context)),
Self::Slider(widget) => parent.append(&widget.into_widget(context)),
Self::Progress(widget) => parent.append(&widget.into_widget(context)),
}
}
}
Expand Down Expand Up @@ -226,8 +226,6 @@ impl Module<gtk::Box> for CustomModule {
}
}

container.show_all();

Some(container)
}
}
1 change: 1 addition & 0 deletions src/modules/custom/slider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use gtk::prelude::*;
use gtk::Scale;
use serde::Deserialize;
use std::cell::Cell;
use glib::signal::Inhibit;
use tokio::spawn;
use tracing::error;

Expand Down
4 changes: 2 additions & 2 deletions src/modules/focused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ impl Module<gtk::Box> for FocusedModule {
truncate.truncate_label(&label);
}

container.add(&icon);
container.add(&label);
container.append(&icon);
container.append(&label);

{
let icon_theme = icon_theme.clone();
Expand Down
3 changes: 1 addition & 2 deletions src/modules/launcher/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use gtk::{Button, IconTheme, Orientation};
use indexmap::IndexMap;
use std::rc::Rc;
use std::sync::RwLock;
use glib::signal::Inhibit;
use tokio::sync::mpsc::Sender;
use tracing::error;

Expand Down Expand Up @@ -227,8 +228,6 @@ impl ItemButton {
});
}

button.show_all();

Self {
button,
persistent: item.favorite,
Expand Down
7 changes: 3 additions & 4 deletions src/modules/launcher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ impl Module<gtk::Box> for LauncherModule {
&controller_tx,
);

container.add(&button.button);
container.append(&button.button);
buttons.insert(item.app_id, button);
}
}
Expand Down Expand Up @@ -421,7 +421,7 @@ impl Module<gtk::Box> for LauncherModule {
// we need some content to force the container to have a size
let placeholder = Button::with_label("PLACEHOLDER");
placeholder.set_width_request(MAX_WIDTH);
container.add(&placeholder);
container.append(&placeholder);

let mut buttons = IndexMap::<String, IndexMap<usize, Button>>::new();

Expand Down Expand Up @@ -513,10 +513,9 @@ impl Module<gtk::Box> for LauncherModule {
if let Some(buttons) = buttons.get(&app_id) {
for (_, button) in buttons {
button.style_context().add_class("popup-item");
container.add(button);
container.append(button);
}

container.show_all();
container.set_width_request(MAX_WIDTH);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/modules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ fn setup_receiver<TSend>(
pub fn wrap_widget<W: IsA<Widget>>(widget: &W, common: CommonConfig) -> EventBox {
let container = EventBox::new();
container.add_events(EventMask::SCROLL_MASK);
container.add(widget);
container.append(widget);

common.install(&container);

Expand Down
Loading

0 comments on commit f94939e

Please sign in to comment.