Skip to content

Commit

Permalink
Merge pull request #73 from gabm/notification
Browse files Browse the repository at this point in the history
Use dbus notifications instead of toast overlay
  • Loading branch information
gabm authored Apr 4, 2024
2 parents 1ba60d6 + aafe53d commit 3226fb4
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 138 deletions.
13 changes: 2 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{io, time::Duration};
use configuration::{Configuration, APP_CONFIG};
use gdk_pixbuf::{Pixbuf, PixbufLoader};
use gtk::prelude::*;

use relm4::gtk::gdk::Rectangle;

use relm4::{
Expand All @@ -15,13 +16,13 @@ use relm4::{
use anyhow::{anyhow, Context, Result};

use sketch_board::SketchBoardOutput;
use ui::toast::Toast;
use ui::toolbars::{StyleToolbar, StyleToolbarInput, ToolsToolbar, ToolsToolbarInput};

mod command_line;
mod configuration;
mod femtovg_area;
mod math;
mod notification;
mod sketch_board;
mod style;
mod tools;
Expand All @@ -33,15 +34,13 @@ use crate::sketch_board::{KeyEventMsg, SketchBoard, SketchBoardInput};
struct App {
image_dimensions: (i32, i32),
sketch_board: Controller<SketchBoard>,
toast: Controller<Toast>,
tools_toolbar: Controller<ToolsToolbar>,
style_toolbar: Controller<StyleToolbar>,
}

#[derive(Debug)]
enum AppInput {
Realized,
ShowToast(String),
ToggleToolbarsDisplay,
}

Expand Down Expand Up @@ -185,8 +184,6 @@ impl Component for App {

add_overlay = model.style_toolbar.widget(),

add_overlay = model.toast.widget(),

model.sketch_board.widget(),
}
}
Expand All @@ -195,7 +192,6 @@ impl Component for App {
fn update(&mut self, message: Self::Input, sender: ComponentSender<Self>, root: &Self::Root) {
match message {
AppInput::Realized => self.resize_window_initial(root, sender),
AppInput::ShowToast(msg) => self.toast.emit(ui::toast::ToastMessage::Show(msg)),
AppInput::ToggleToolbarsDisplay => {
self.tools_toolbar
.sender()
Expand Down Expand Up @@ -227,15 +223,11 @@ impl Component for App {

let image_dimensions = (image.width(), image.height());

// Toast
let toast = Toast::builder().launch(3000).detach();

// SketchBoard
let sketch_board =
SketchBoard::builder()
.launch(image)
.forward(sender.input_sender(), |t| match t {
SketchBoardOutput::ShowToast(msg) => AppInput::ShowToast(msg),
SketchBoardOutput::ToggleToolbarsDisplay => AppInput::ToggleToolbarsDisplay,
});

Expand All @@ -253,7 +245,6 @@ impl Component for App {
// Model
let model = App {
sketch_board,
toast,
tools_toolbar,
style_toolbar,
image_dimensions,
Expand Down
36 changes: 36 additions & 0 deletions src/notification.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use gdk_pixbuf::gio::FileIcon;
use relm4::gtk::gio::{prelude::ApplicationExt, Notification};

use relm4::gtk::{IconLookupFlags, IconTheme, TextDirection};

pub fn log_result(msg: &str) {
println!("{}", msg);
show_notification(msg);
}

fn show_notification(msg: &str) {
// construct
let notification = Notification::new("Satty");
notification.set_body(Some(msg));

// lookup sattys icon
let theme = IconTheme::default();
if theme.has_icon("satty") {
if let Some(icon_file) = theme
.lookup_icon(
"satty",
&[],
96,
1,
TextDirection::Ltr,
IconLookupFlags::empty(),
)
.file()
{
notification.set_icon(&FileIcon::new(&icon_file));
}
}

// send notification
relm4::main_application().send_notification(None, &notification);
}
43 changes: 13 additions & 30 deletions src/sketch_board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use relm4::{gtk, Component, ComponentParts, ComponentSender};
use crate::configuration::APP_CONFIG;
use crate::femtovg_area::FemtoVGArea;
use crate::math::Vec2D;
use crate::notification::log_result;
use crate::style::Style;
use crate::tools::{Tool, ToolEvent, ToolUpdateResult, ToolsManager};
use crate::ui::toolbars::ToolbarEvent;
Expand All @@ -39,7 +40,6 @@ pub enum Action {

#[derive(Debug, Clone)]
pub enum SketchBoardOutput {
ShowToast(String),
ToggleToolbarsDisplay,
}

Expand Down Expand Up @@ -162,24 +162,17 @@ impl SketchBoard {
)
}

fn handle_render_result(
&self,
sender: ComponentSender<Self>,
image: RenderedImage,
action: Action,
) {
fn handle_render_result(&self, image: RenderedImage, action: Action) {
match action {
Action::SaveToClipboard => {
self.handle_copy_clipboard(sender, Self::image_to_pixbuf(image))
}
Action::SaveToFile => self.handle_save(sender, Self::image_to_pixbuf(image)),
Action::SaveToClipboard => self.handle_copy_clipboard(Self::image_to_pixbuf(image)),
Action::SaveToFile => self.handle_save(Self::image_to_pixbuf(image)),
};
if APP_CONFIG.read().early_exit() {
relm4::main_application().quit();
}
}

fn handle_save(&self, sender: ComponentSender<Self>, image: Pixbuf) {
fn handle_save(&self, image: Pixbuf) {
let output_filename = match APP_CONFIG.read().output_filename() {
None => {
println!("No Output filename specified!");
Expand All @@ -193,11 +186,7 @@ impl SketchBoard {

// TODO: we could support more data types
if !output_filename.ends_with(".png") {
let msg = "The only supported format is png, but the filename does not end in png";
println!("{msg}");
sender
.output_sender()
.emit(SketchBoardOutput::ShowToast(msg.to_string()));
log_result("The only supported format is png, but the filename does not end in png");
return;
}

Expand All @@ -209,14 +198,10 @@ impl SketchBoard {
}
};

let msg = match fs::write(&output_filename, data) {
Err(e) => format!("Error while saving file: {e}"),
Ok(_) => format!("File saved to '{}'.", &output_filename),
match fs::write(&output_filename, data) {
Err(e) => log_result(&format!("Error while saving file: {e}")),
Ok(_) => log_result(&format!("File saved to '{}'.", &output_filename)),
};

sender
.output_sender()
.emit(SketchBoardOutput::ShowToast(msg));
}

fn save_to_clipboard(&self, texture: &impl IsA<Texture>) -> anyhow::Result<()> {
Expand Down Expand Up @@ -248,7 +233,7 @@ impl SketchBoard {
Ok(())
}

fn handle_copy_clipboard(&self, sender: ComponentSender<Self>, image: Pixbuf) {
fn handle_copy_clipboard(&self, image: Pixbuf) {
let texture = Texture::for_pixbuf(&image);

let result = if let Some(command) = APP_CONFIG.read().copy_command() {
Expand All @@ -260,13 +245,11 @@ impl SketchBoard {
match result {
Err(e) => println!("Error saving {e}"),
Ok(()) => {
sender.output_sender().emit(SketchBoardOutput::ShowToast(
"Copied to clipboard.".to_string(),
));
log_result("Copied to clipboard.");

// TODO: rethink order and messaging patterns
if APP_CONFIG.read().save_after_copy() {
self.handle_save(sender, image);
self.handle_save(image);
};
}
}
Expand Down Expand Up @@ -456,7 +439,7 @@ impl Component for SketchBoard {
self.handle_toolbar_event(toolbar_event)
}
SketchBoardInput::RenderResult(img, action) => {
self.handle_render_result(sender, img, action);
self.handle_render_result(img, action);
ToolUpdateResult::Unmodified
}
};
Expand Down
1 change: 0 additions & 1 deletion src/ui/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
pub mod toast;
pub mod toolbars;
96 changes: 0 additions & 96 deletions src/ui/toast.rs

This file was deleted.

0 comments on commit 3226fb4

Please sign in to comment.