Skip to content

Commit

Permalink
update: toolbar style
Browse files Browse the repository at this point in the history
  • Loading branch information
Horbin-Magician committed Aug 14, 2024
1 parent acf5495 commit a93f2e5
Show file tree
Hide file tree
Showing 3 changed files with 220 additions and 222 deletions.
65 changes: 37 additions & 28 deletions src/module/screen_shotter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod toolbar;
use arboard::Clipboard;
use image::{self, GenericImageView, Rgba};
use std::{sync::{Arc, Mutex, mpsc, mpsc::Sender}, collections::HashMap};
use slint::{SharedPixelBuffer, Rgba8Pixel};
use slint::{Rgba8Pixel, SharedPixelBuffer, Weak};
use i_slint_backend_winit::{winit::platform::windows::WindowExtWindows, WinitWindowAccessor};
use global_hotkey::hotkey::{HotKey, Modifiers, Code};
use xcap::Monitor;
Expand All @@ -26,9 +26,8 @@ pub enum PinOperation {
pub enum ShotterMessage {
Move(u32),
Close(u32),
ShowToolbar(i32, i32),
MoveToolbar(f32, f32),
FinishToolbar(u32),
ShowToolbar(i32, i32, u32, Weak<PinWindow>),
HideToolbar(bool),
OperatePin(u32, PinOperation),
}

Expand Down Expand Up @@ -189,7 +188,8 @@ impl ScreenShotter{
let pin_win = PinWin::new(
bac_buffer_rc.clone(), rect,
mask_win.get_offset_x(), mask_win.get_offset_y(),
*max_pin_win_id, message_sender_clone);
*max_pin_win_id, message_sender_clone
);

let pin_window_clone = pin_win.pin_window.as_weak();

Expand All @@ -214,54 +214,59 @@ impl ScreenShotter{
// event listen
let pin_windows_clone = pin_windows.clone();
// let pin_wins_clone = pin_wins.clone();
let toolbar_window_clone = toolbar.get_window();
let toolbar_window_clone: slint::Weak<toolbar::ToolbarWindow> = toolbar.get_window();
std::thread::spawn(move || {
loop {
if let Ok(message) = message_reciever.recv() {
match message {
ShotterMessage::Move(id) => {
ScreenShotter::pin_win_move_hander(pin_windows.clone(), id);
ScreenShotter::pin_win_move_hander(pin_windows.clone(), id, toolbar_window_clone.clone());
},
ShotterMessage::Close(id) => {
pin_windows_clone.lock().unwrap().remove(&id);
// pin_wins_clone.lock().unwrap().remove(&id); // TODO: clear pin_wins
},
ShotterMessage::ShowToolbar(x, y) => {
ShotterMessage::ShowToolbar(x, y, id, pin_window) => {
toolbar_window_clone.upgrade_in_event_loop(move |win| {
win.invoke_show_pos(x, y);
win.invoke_show_pos(x, y, id as i32);
}).unwrap();
},
ShotterMessage::MoveToolbar(x, y) => {
toolbar_window_clone.upgrade_in_event_loop(move |win| {
win.invoke_move(x, y);
// focus the pin window
pin_window.upgrade_in_event_loop(move |win| {
win.window().with_winit_window(|winit_win: &i_slint_backend_winit::winit::window::Window| {
winit_win.focus_window();
winit_win.request_redraw(); // TODO to fix the error win size
});
}).unwrap();
},
ShotterMessage::FinishToolbar(id) => {
ShotterMessage::HideToolbar(if_force) => {
toolbar_window_clone.upgrade_in_event_loop(move |win| {
win.invoke_finish(id as i32);
win.invoke_try_hide(if_force);
}).unwrap();
},
ShotterMessage::OperatePin(id, operation) => {
// toolbar_window_clone.upgrade_in_event_loop(move |win| {
// win.invoke_try_hide(true);
// }).unwrap(); // TODO del?
let pin_windows = pin_windows_clone.lock().unwrap();
if let Some(pin_win) = pin_windows.get(&id) {
if let Some(pin_window) = pin_windows.get(&id) {
match operation {
PinOperation::Close() => {
pin_win.upgrade_in_event_loop(move |win| {
pin_window.upgrade_in_event_loop(move |win| {
win.invoke_close();
}).unwrap();
},
PinOperation::Hide() => {
pin_win.upgrade_in_event_loop(move |win| {
pin_window.upgrade_in_event_loop(move |win| {
win.invoke_hide();
}).unwrap();
},
PinOperation::Save() => {
pin_win.upgrade_in_event_loop(move |win| {
pin_window.upgrade_in_event_loop(move |win| {
win.invoke_save();
}).unwrap();
},
PinOperation::Copy() => {
pin_win.upgrade_in_event_loop(move |win| {
pin_window.upgrade_in_event_loop(move |win| {
win.invoke_copy();
}).unwrap();
},
Expand All @@ -282,22 +287,24 @@ impl ScreenShotter{
}
}

fn pin_win_move_hander(pin_windows: Arc<Mutex<HashMap<u32, slint::Weak<PinWindow>>>>, move_win_id: u32) {
fn pin_win_move_hander(pin_windows: Arc<Mutex<HashMap<u32, slint::Weak<PinWindow>>>>, move_win_id: u32, toolbar_window: slint::Weak<toolbar::ToolbarWindow>) {
slint::invoke_from_event_loop(move || {
let padding = 10;
let pin_windows = pin_windows.lock().unwrap();
let move_win = &pin_windows[&move_win_id].unwrap();

let move_pos = move_win.window().position();
let move_size = move_win.window().size();
let move_bottom = move_pos.y + move_size.height as i32;
let move_right = move_pos.x + move_size.width as i32;

toolbar_window.unwrap().invoke_win_move(move_right, move_bottom);

for pin_win_id in pin_windows.keys(){
if move_win_id != *pin_win_id {
let other_win = &pin_windows[pin_win_id].unwrap();

let move_pos = move_win.window().position();
let move_size = move_win.window().size();
let other_pos = other_win.window().position();
let other_size = other_win.window().size();

let move_bottom = move_pos.y + move_size.height as i32;
let move_right = move_pos.x + move_size.width as i32;
let other_bottom = other_pos.y + other_size.height as i32;
let other_right = other_pos.x + other_size.width as i32;

Expand Down Expand Up @@ -334,7 +341,9 @@ impl ScreenShotter{
}
}

move_win.window().set_position(slint::PhysicalPosition::new(move_pos.x - delta_x, move_pos.y - delta_y));
if delta_x != 0 || delta_y != 0 {
move_win.window().set_position(slint::PhysicalPosition::new(move_pos.x - delta_x, move_pos.y - delta_y));
}
}
}
}).unwrap();
Expand Down
118 changes: 61 additions & 57 deletions src/module/screen_shotter/pin_win.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ impl PinWin {
let pin_window_clone = pin_window.as_weak();
let message_sender_clone = message_sender.clone();
pin_window.on_win_move(move |mut delta_x, mut delta_y| {

let pin_window_clone = pin_window_clone.unwrap();
let now_pos = pin_window_clone.window().position().to_logical(pin_window_clone.window().scale_factor());
let is_stick_x = pin_window_clone.get_is_stick_x();
Expand Down Expand Up @@ -67,17 +66,46 @@ impl PinWin {
});
}

{ // code for focuse change
let pin_window_clone = pin_window.as_weak();
let message_sender_clone = message_sender.clone();
pin_window.on_focus_trick(
move |has_focus| {
let pin_window = pin_window_clone.unwrap();
if has_focus {
let position = pin_window.window().position();
let scale_factor = pin_window.get_scale_factor();
let width = pin_window.get_win_width();
let height = pin_window.get_win_height();
let left_bottom_x = position.x + (width * scale_factor) as i32;
let left_bottom_y = position.y + (height * scale_factor) as i32;
message_sender_clone.send(ShotterMessage::ShowToolbar(left_bottom_x, left_bottom_y, id, pin_window.as_weak())).unwrap();
} else {
if pin_window.window().is_visible() == false || pin_window.window().is_minimized() {
message_sender_clone.send(ShotterMessage::HideToolbar(true)).unwrap();
} else {
message_sender_clone.send(ShotterMessage::HideToolbar(false)).unwrap();
}
}
true
}
);
}

{ // code for function
{ // for close
{ // for close and hide
let pin_window_clone = pin_window.as_weak();
let message_sender_clone = message_sender.clone();
pin_window.on_close(move || {
message_sender_clone.send(ShotterMessage::HideToolbar(true)).unwrap();
pin_window_clone.unwrap().hide().unwrap();
message_sender_clone.send(ShotterMessage::Close(id)).unwrap();
});

let pin_window_clone = pin_window.as_weak();
let message_sender_clone = message_sender.clone();
pin_window.on_hide(move || {
message_sender_clone.send(ShotterMessage::HideToolbar(true)).unwrap();
pin_window_clone.unwrap().window().with_winit_window(|winit_win: &i_slint_backend_winit::winit::window::Window| {
winit_win.set_minimized(true);
});
Expand All @@ -102,26 +130,25 @@ impl PinWin {
);
img = img.crop(img_x as u32, img_y as u32, img_width as u32, img_height as u32);

let app_config = AppConfig::global().lock().unwrap();
let save_path = app_config.get_save_path();

let file_name = chrono::Local::now().format("Rotor_%Y-%m-%d-%H-%M-%S.png").to_string();
let params = DialogParams {
title: "Select an image to save",
file_types: vec![("PNG Files", "*.png")],
default_extension: "png",
file_name: &file_name,
default_folder: &save_path,
..Default::default()
};
pin_window_clone.unwrap().hide().unwrap();

let dialog_result = wfd::save_dialog(params);
if let Ok(file_path_result) = dialog_result {
img.save(file_path_result.selected_file_path).unwrap();
}

pin_window_clone.unwrap().invoke_close();

std::thread::spawn(move || {
let app_config = AppConfig::global().lock().unwrap();
let save_path = app_config.get_save_path();
let file_name = chrono::Local::now().format("Rotor_%Y-%m-%d-%H-%M-%S.png").to_string();
let params = DialogParams {
title: "Select an image to save",
file_types: vec![("PNG Files", "*.png")],
default_extension: "png",
file_name: &file_name,
default_folder: &save_path,
..Default::default()
};
let dialog_result = wfd::save_dialog(params);
if let Ok(file_path_result) = dialog_result {
img.save(file_path_result.selected_file_path).unwrap();
}
});
});

//copy
Expand All @@ -135,43 +162,18 @@ impl PinWin {
).unwrap()
);
img = img.crop(img_x as u32, img_y as u32, img_width as u32, img_height as u32);

let mut clipboard = Clipboard::new().unwrap();
let img_data = ImageData {
width: img.width() as usize,
height: img.height() as usize,
bytes: Cow::from(img.to_rgba8().to_vec())
};
clipboard.set_image(img_data).unwrap();

pin_window_clone.unwrap().invoke_close();
});
}

{ // for show toolbar
let pin_window_clone = pin_window.as_weak();
let message_sender_clone = message_sender.clone();
pin_window.on_show_toolbar(move |x, y| {
let pin_window = pin_window_clone.unwrap();
let position = pin_window.window().position();
let scale = pin_window.window().scale_factor();
let center_x = position.x + (x * scale) as i32;
let center_y = position.y + (y * scale) as i32;
message_sender_clone.send(ShotterMessage::ShowToolbar(center_x, center_y)).unwrap();
});
}

{ // for move toolbar
let message_sender_clone = message_sender.clone();
pin_window.on_move_toolbar(move |x, y| {
message_sender_clone.send(ShotterMessage::MoveToolbar(x, y)).unwrap();
});
}

{ // for finish toolbar
let message_sender_clone = message_sender.clone();
pin_window.on_finish_toolbar(move || {
message_sender_clone.send(ShotterMessage::FinishToolbar(id)).unwrap();
std::thread::spawn(move || {
let mut clipboard = Clipboard::new().unwrap();
let img_data = ImageData {
width: img.width() as usize,
height: img.height() as usize,
bytes: Cow::from(img.to_rgba8().to_vec())
};
clipboard.set_image(img_data).unwrap();
});
});
}
}
Expand Down Expand Up @@ -206,11 +208,13 @@ slint::slint! {

export component PinWindow inherits Window {
no-frame: true;
always-on-top: true;
title: "小云视窗";
forward-focus: key_focus;
forward-focus: key-focus;
icon: @image-url("assets/logo.png");

pure callback focus_trick(bool) -> bool;
always-on-top: focus_trick(key-focus.has-focus);

in property <image> bac_image;
in property <length> win_border_width: 1px;
in property <float> scale_factor;
Expand Down
Loading

0 comments on commit a93f2e5

Please sign in to comment.