Skip to content

Commit

Permalink
update: change zoom_delta to 2, and can set it now
Browse files Browse the repository at this point in the history
  • Loading branch information
Horbin-Magician committed Oct 8, 2024
1 parent f1861be commit d59890b
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 3 deletions.
13 changes: 13 additions & 0 deletions src/core/application/app_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ pub struct Config {
save_path: String,
#[serde(default = "default_shortcuts")]
shortcuts: HashMap<String, String>,
#[serde(default = "default_zoom_delta")]
zoom_delta: u8,
}

fn default_false() -> bool { false }
fn default_u8() -> u8 { 0 }
fn default_zoom_delta() -> u8 { 2 }
fn default_string() -> String { String::new() }
fn default_shortcuts() -> HashMap<String, String> {
let mut shortcuts = HashMap::new();
Expand Down Expand Up @@ -142,6 +145,16 @@ impl AppConfig {
}
None
}

pub fn set_zoom_delta(&mut self, delta: u8) -> Result<(), Box<dyn Error>> {
self.config.zoom_delta = delta;
self.save()?;
Ok(())
}

pub fn get_zoom_delta(&self) -> u8 {
self.config.zoom_delta
}
}

static INSTANCE: LazyLock<Mutex<AppConfig>> = LazyLock::new(|| {
Expand Down
6 changes: 6 additions & 0 deletions src/module/screen_shotter/pin_win.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ impl PinWin {
pin_window.set_img_height(rect.height / scale_factor);
pin_window.set_scale_factor(scale_factor);

let zoom_delta = AppConfig::global()
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner())
.get_zoom_delta();
pin_window.set_zoom_delta(zoom_delta.into());

{ // code for window move
let pin_window_clone = pin_window.as_weak();
let message_sender_clone = message_sender.clone();
Expand Down
17 changes: 17 additions & 0 deletions src/module/setting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ impl Setting {
setting_win.set_shortcut_pinwin_close(app_config.get_shortcut("pinwin_close").unwrap_or(&"unkown".to_string()).into());
setting_win.set_shortcut_pinwin_copy(app_config.get_shortcut("pinwin_copy").unwrap_or(&"unkown".to_string()).into());
setting_win.set_shortcut_pinwin_hide(app_config.get_shortcut("pinwin_hide").unwrap_or(&"unkown".to_string()).into());

setting_win.set_zoom_delta(app_config.get_zoom_delta().to_string().into());

{ // code for setting change
{ // power boot
Expand All @@ -104,6 +106,21 @@ impl Setting {
});
}

{ // screenshot
let setting_win_clone = setting_win.as_weak();
setting_win.on_zoom_delta_changed(move |zoom_delta| {
let zoom_delta_int = zoom_delta.parse::<u8>().unwrap_or(2);
if let Some(setting_win) = setting_win_clone.upgrade() {
setting_win.set_zoom_delta(zoom_delta_int.to_string().into());
}
AppConfig::global()
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner())
.set_zoom_delta(zoom_delta_int)
.unwrap_or_else(|e| log_util::log_error(format!("Failed to set zoom delta: {:?}", e)));
});
}

{// shortcut
let setting_win_clone = setting_win.as_weak();
let msg_sender = msg_sender.clone();
Expand Down
7 changes: 4 additions & 3 deletions src/ui/screen_shotter/pin_win.slint
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export component PinWindow inherits BaseWindow {

in property <length> img_x;
in property <length> img_y;
in-out property <int> zoom_factor: 100; // neet to be divided by one hundred
in-out property <int> zoom_factor: 100; // neet to be divided by one hundred
in-out property <int> zoom_delta: 2; // neet to be divided by one hundred
in-out property <length> img_width;
in-out property <length> img_height;

Expand Down Expand Up @@ -151,9 +152,9 @@ export component PinWindow inherits BaseWindow {

scroll-event(event) => {
if (event.delta-y > 0) {
if (root.zoom_factor < 500) { root.zoom_factor = root.zoom_factor + 1; }
if (root.zoom_factor < 500) { root.zoom_factor = root.zoom_factor + zoom_delta; }
} else if (event.delta-y < 0) {
if (root.zoom_factor > 10) { root.zoom_factor = root.zoom_factor - 1; }
if (root.zoom_factor > 10) { root.zoom_factor = root.zoom_factor - zoom_delta; }
}
root.show_zoom = true;
show_zoom_timer.running = true;
Expand Down
29 changes: 29 additions & 0 deletions src/ui/setting/pages/screen_shotter_page.slint
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ export component ScreenShotterSettingPage inherits Page {
in property <string> shortcut_pinwin_close;
in property <string> shortcut_pinwin_copy;
in property <string> shortcut_pinwin_hide;
in property <string> zoom_delta;

callback zoom_delta_changed(string);
callback shortcut_changed(string, KeyEvent);

GroupBox {
Expand Down Expand Up @@ -179,6 +181,33 @@ export component ScreenShotterSettingPage inherits Page {
}
}

GroupBox {
title: @tr("贴图");
VerticalLayout {
padding-left: 20px;
padding-right: 50px;
spacing: 5px;
HorizontalLayout {
alignment: space-between;
Text {
text: @tr("缩放灵敏度 (%)");
height: 30px;
width: 100px;
vertical-alignment: center;
}
LineEdit {
height: 30px;
width: 150px;
input-type: decimal;
text: zoom_delta;
edited() => {
zoom_delta_changed(self.text);
}
}
}
}
}

GroupBox {
title: @tr("其他");
VerticalLayout {
Expand Down
4 changes: 4 additions & 0 deletions src/ui/setting/setting_win.slint
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export component SettingWindow inherits BaseWindow {
callback update();
callback shortcut_changed(string, KeyEvent);
callback click_logo <=> side_bar.click_logo;
callback zoom_delta_changed(string);

in property <string> version;

Expand All @@ -40,6 +41,7 @@ export component SettingWindow inherits BaseWindow {
in property <string> shortcut_pinwin_close;
in property <string> shortcut_pinwin_copy;
in property <string> shortcut_pinwin_hide;
in property <string> zoom_delta;

in-out property <int> update_state: 0;
in-out property <string> current_version;
Expand Down Expand Up @@ -109,7 +111,9 @@ export component SettingWindow inherits BaseWindow {
shortcut_pinwin_close: root.shortcut_pinwin_close;
shortcut_pinwin_copy: root.shortcut_pinwin_copy;
shortcut_pinwin_hide: root.shortcut_pinwin_hide;
zoom_delta: root.zoom_delta;
shortcut_changed(shortcut, event) => { root.shortcut_changed(shortcut, event); }
zoom_delta_changed(zoom_delta) => { root.zoom_delta_changed(zoom_delta); }
}
}
}
Expand Down

0 comments on commit d59890b

Please sign in to comment.