From eb7d2a467624e1313b7bdb4e8de3583dde026010 Mon Sep 17 00:00:00 2001 From: sdaqo Date: Sun, 4 Feb 2024 15:37:19 +0100 Subject: [PATCH] add size lock (#4) --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/app/ctxmenu/mod.rs | 45 +++++++++++++++++++++++++++++++++++++++++- src/app/mod.rs | 20 +++++++++++-------- src/config.rs | 2 ++ 5 files changed, 60 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 352ac8e..5dd0ebf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -852,7 +852,7 @@ dependencies = [ [[package]] name = "mpv-subs-popout" -version = "0.3.1" +version = "0.3.2" dependencies = [ "glib-build-tools", "gtk", diff --git a/Cargo.toml b/Cargo.toml index ea74eef..70ff50f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mpv-subs-popout" -version = "0.4.0" +version = "0.3.2" edition = "2021" description = "Popout Mpv's Subs" readme = "README.md" diff --git a/src/app/ctxmenu/mod.rs b/src/app/ctxmenu/mod.rs index 0aee3a3..322958c 100644 --- a/src/app/ctxmenu/mod.rs +++ b/src/app/ctxmenu/mod.rs @@ -99,6 +99,44 @@ pub fn build_ctxmenu(window: &MpvSubsWindow) -> ContextMenu { None, ); + let sizelock_btn = CheckButton::builder() + .label("Lock Size") + .active(config.size_lock != None) + .build(); + + ctxmenu.add_item( + &sizelock_btn, + Box::new( + clone!(@weak window => @default-return Inhibit(true), move |wg, _ev| { + let state = wg.is_active(); + + wg.set_active(!state); + + let mut config = AppConfig::new(); + let label_box = window.imp().label_box.get().unwrap(); + if !state { + let width = label_box.allocated_width(); + let height = label_box.allocated_height(); + + label_box.set_size_request(width, height); + window.imp().sub_label.get().unwrap().set_wrap(true); + window.imp().sub_label.get().unwrap().set_wrap(true); + + config.size_lock = Some((width, height)); + } else { + label_box.set_size_request(0, 0); + window.imp().sub_label.get().unwrap().set_wrap(false); + window.imp().sub_label.get().unwrap().set_wrap(false); + config.size_lock = None; + } + + config.save(); + Inhibit(true) + }), + ), + None, + ); + let auto_tl_btn = CheckButton::builder() .label("Auto Translate") .active(config.auto_tl) @@ -110,13 +148,18 @@ pub fn build_ctxmenu(window: &MpvSubsWindow) -> ContextMenu { clone!(@weak window => @default-return Inhibit(true), move |wg, _ev| { let state = wg.is_active(); - wg.set_active(!state); let _ = window.imp().channel_sender.get().unwrap().send(Message::SetTlLabelVisibilty(!state)); let _ = window.imp().channel_sender.get().unwrap().send(Message::UpdateTlLabel("".to_string())); let mut config = AppConfig::new(); + if let Some(size) = config.size_lock { + window.imp().label_box.get().unwrap().set_size_request(size.0, size.1); + window.imp().sub_label.get().unwrap().set_wrap(true); + window.imp().sub_label.get().unwrap().set_wrap(true); + } + config.auto_tl = !state; config.save(); diff --git a/src/app/mod.rs b/src/app/mod.rs index b42f3da..66cb64b 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -28,25 +28,21 @@ pub fn build_window(app: >k::Application) -> MpvSubsWindow { gtk::STYLE_PROVIDER_PRIORITY_APPLICATION, ); + let cfg = AppConfig::new(); + let ctx_menu = build_ctxmenu(&window); - - let label_box = gtk::Box::new(gtk::Orientation::Vertical, 6); + let label_box = gtk::Box::new(gtk::Orientation::Vertical, 6); label_box.set_homogeneous(true); let sub_label = Label::builder() .name("sub_label") .selectable(true) .build(); - sub_label.style_context().add_class("sub_label"); ctx_menu.attach_to_widget(&sub_label); - - window.imp().sub_label.set(sub_label).ok(); - label_box.add(window.imp().sub_label.get().unwrap()); - let cfg = AppConfig::new(); let tl_label = Label::builder() .name("tl_label") @@ -54,8 +50,16 @@ pub fn build_window(app: >k::Application) -> MpvSubsWindow { tl_label.style_context().add_class("sub_label"); - ctx_menu.attach_to_widget(&tl_label); + if let Some(size) = cfg.size_lock { + label_box.set_size_request(size.0, size.1); + sub_label.set_wrap(true); + tl_label.set_wrap(true); + } + window.imp().sub_label.set(sub_label).ok(); + label_box.add(window.imp().sub_label.get().unwrap()); + + ctx_menu.attach_to_widget(&tl_label); window.imp().tl_label.set(tl_label).ok(); if cfg.auto_tl { diff --git a/src/config.rs b/src/config.rs index 78b4d7d..c28ae46 100644 --- a/src/config.rs +++ b/src/config.rs @@ -18,6 +18,7 @@ pub struct AppConfig { pub borders: bool, pub bg_col: String, pub text_col: String, + pub size_lock: Option<(i32, i32)>, pub auto_tl: bool, pub default_tl_engine: String, pub translators: Vec @@ -32,6 +33,7 @@ impl AppConfig { docked: false, ontop: true, borders: true, + size_lock: None, bg_col: "rgb(42, 46, 50)".to_owned(), text_col: "rgb(255, 255, 255)".to_owned(), auto_tl: false,