Merge pull request #2235 from tristan957/toast

apprt/gtk: use toast UX from Adwaita if available
This commit is contained in:
Mitchell Hashimoto
2024-09-16 10:22:09 -07:00
committed by GitHub
2 changed files with 38 additions and 1 deletions

View File

@ -353,6 +353,13 @@ pub fn reloadConfig(self: *App) !?*const Config {
log.warn("error handling configuration changes err={}", .{err});
};
if (adwaita.enabled(&self.config)) {
if (self.core_app.focusedSurface()) |core_surface| {
const surface = core_surface.rt_surface;
if (surface.container.window()) |window| window.onConfigReloaded();
}
}
return &self.config;
}

View File

@ -41,6 +41,10 @@ notebook: Notebook,
context_menu: *c.GtkWidget,
/// The libadwaita widget for receiving toast send requests. If libadwaita is
/// not used, this is null and unused.
toast_overlay: ?*c.GtkWidget,
pub fn create(alloc: Allocator, app: *App) !*Window {
// Allocate a fixed pointer for our window. We try to minimize
// allocations but windows and other GUI requirements are so minimal
@ -63,6 +67,7 @@ pub fn init(self: *Window, app: *App) !void {
.header = null,
.notebook = undefined,
.context_menu = undefined,
.toast_overlay = undefined,
};
// Create the window
@ -203,7 +208,17 @@ pub fn init(self: *Window, app: *App) !void {
// Setup our notebook
self.notebook = Notebook.create(self);
c.gtk_box_append(@ptrCast(box), self.notebook.asWidget());
// Setup our toast overlay if we have one
self.toast_overlay = if (self.isAdwWindow()) toast: {
const toast_overlay = c.adw_toast_overlay_new();
c.adw_toast_overlay_set_child(
@ptrCast(toast_overlay),
@ptrCast(@alignCast(self.notebook.asWidget())),
);
c.gtk_box_append(@ptrCast(box), toast_overlay);
break :toast toast_overlay;
} else null;
// If we have a tab overview then we can set it on our notebook.
if (tab_overview_) |tab_overview| {
@ -462,6 +477,17 @@ pub fn focusCurrentTab(self: *Window) void {
_ = c.gtk_widget_grab_focus(gl_area);
}
pub fn onConfigReloaded(self: *Window) void {
self.sendToast("Reloaded the configuration");
}
fn sendToast(self: *Window, title: [:0]const u8) void {
const toast_overlay = self.toast_overlay orelse return;
const toast = c.adw_toast_new(title);
c.adw_toast_set_timeout(toast, 3);
c.adw_toast_overlay_add_toast(@ptrCast(toast_overlay), toast);
}
// Note: we MUST NOT use the GtkButton parameter because gtkActionNewTab
// sends an undefined value.
fn gtkTabNewClick(_: *c.GtkButton, ud: ?*anyopaque) callconv(.C) void {
@ -697,6 +723,10 @@ fn gtkActionCopy(
log.warn("error performing binding action error={}", .{err});
return;
};
if (self.isAdwWindow()) {
self.sendToast("Copied to clipboard");
}
}
fn gtkActionPaste(