#![allow(unsafe_op_in_unsafe_fn)]
use windows_sys::Win32::Foundation::{HWND, RECT};
use windows_sys::Win32::UI::WindowsAndMessaging::{
SetWindowPos, SWP_NOZORDER, SendMessageW, WM_SETFONT,
};
use windows_sys::Win32::Graphics::Gdi::HFONT;
use super::base::Component;
use crate::ui::builder::ControlBuilder;
use crate::ui::controls::{apply_button_theme, apply_combobox_theme, apply_accent_button_theme};
pub struct ActionPanelIds {
pub btn_files: u16,
pub btn_folder: u16,
pub btn_remove: u16,
pub lbl_input: u16,
pub combo_action_mode: u16,
pub lbl_action_mode: u16,
pub combo_algo: u16,
pub lbl_algo: u16,
pub chk_force: u16,
pub btn_process: u16,
pub btn_cancel: u16,
}
pub struct ActionPanel {
hwnd_lbl_input: HWND,
hwnd_files: HWND,
hwnd_folder: HWND,
hwnd_remove: HWND,
hwnd_lbl_action_mode: HWND,
hwnd_action_mode: HWND,
hwnd_lbl_algo: HWND,
hwnd_combo: HWND,
hwnd_force: HWND,
hwnd_process: HWND,
hwnd_cancel: HWND,
ids: ActionPanelIds,
}
impl ActionPanel {
pub fn new(ids: ActionPanelIds) -> Self {
Self {
hwnd_lbl_input: std::ptr::null_mut(),
hwnd_files: std::ptr::null_mut(),
hwnd_folder: std::ptr::null_mut(),
hwnd_remove: std::ptr::null_mut(),
hwnd_lbl_action_mode: std::ptr::null_mut(),
hwnd_action_mode: std::ptr::null_mut(),
hwnd_lbl_algo: std::ptr::null_mut(),
hwnd_combo: std::ptr::null_mut(),
hwnd_force: std::ptr::null_mut(),
hwnd_process: std::ptr::null_mut(),
hwnd_cancel: std::ptr::null_mut(),
ids,
}
}
#[inline]
pub fn files_hwnd(&self) -> HWND {
self.hwnd_files
}
#[inline]
pub fn folder_hwnd(&self) -> HWND {
self.hwnd_folder
}
#[inline]
pub fn remove_hwnd(&self) -> HWND {
self.hwnd_remove
}
#[inline]
pub fn action_mode_hwnd(&self) -> HWND {
self.hwnd_action_mode
}
#[inline]
pub fn combo_hwnd(&self) -> HWND {
self.hwnd_combo
}
#[inline]
pub fn force_hwnd(&self) -> HWND {
self.hwnd_force
}
#[inline]
pub fn process_hwnd(&self) -> HWND {
self.hwnd_process
}
#[inline]
pub fn cancel_hwnd(&self) -> HWND {
self.hwnd_cancel
}
pub unsafe fn set_font(&self, hfont: HFONT) {
let wparam = hfont as usize;
let lparam = 1;
SendMessageW(self.hwnd_lbl_input, WM_SETFONT, wparam, lparam);
SendMessageW(self.hwnd_files, WM_SETFONT, wparam, lparam);
SendMessageW(self.hwnd_folder, WM_SETFONT, wparam, lparam);
SendMessageW(self.hwnd_remove, WM_SETFONT, wparam, lparam);
SendMessageW(self.hwnd_lbl_action_mode, WM_SETFONT, wparam, lparam);
SendMessageW(self.hwnd_action_mode, WM_SETFONT, wparam, lparam);
SendMessageW(self.hwnd_lbl_algo, WM_SETFONT, wparam, lparam);
SendMessageW(self.hwnd_combo, WM_SETFONT, wparam, lparam);
SendMessageW(self.hwnd_force, WM_SETFONT, wparam, lparam);
SendMessageW(self.hwnd_process, WM_SETFONT, wparam, lparam);
SendMessageW(self.hwnd_cancel, WM_SETFONT, wparam, lparam);
}
}
impl Component for ActionPanel {
unsafe fn create(&mut self, parent: HWND) -> Result<(), String> { unsafe {
let btn_h = 32;
let btn_y = 460;
let is_dark = crate::ui::theme::is_system_dark_mode();
self.hwnd_lbl_input = ControlBuilder::new(parent, self.ids.lbl_input)
.label(false)
.text("Input")
.pos(10, btn_y - 18)
.size(175, 16)
.dark_mode(is_dark)
.build();
self.hwnd_files = ControlBuilder::new(parent, self.ids.btn_files)
.button()
.text("Files")
.pos(10, btn_y)
.size(65, btn_h)
.dark_mode(is_dark)
.build();
self.hwnd_folder = ControlBuilder::new(parent, self.ids.btn_folder)
.button()
.text("Folder")
.pos(85, btn_y)
.size(65, btn_h)
.dark_mode(is_dark)
.build();
self.hwnd_remove = ControlBuilder::new(parent, self.ids.btn_remove)
.button()
.text("Remove")
.pos(160, btn_y)
.size(70, btn_h)
.dark_mode(is_dark)
.build();
self.hwnd_lbl_action_mode = ControlBuilder::new(parent, self.ids.lbl_action_mode)
.label(false)
.text("Action")
.pos(240, btn_y - 18)
.size(100, 16)
.dark_mode(is_dark)
.build();
self.hwnd_action_mode = ControlBuilder::new(parent, self.ids.combo_action_mode)
.combobox()
.pos(240, btn_y)
.size(100, 200) .dark_mode(is_dark)
.build();
self.hwnd_lbl_algo = ControlBuilder::new(parent, self.ids.lbl_algo)
.label(false)
.text("Algorithm")
.pos(350, btn_y - 18)
.size(100, 16)
.dark_mode(is_dark)
.build();
self.hwnd_combo = ControlBuilder::new(parent, self.ids.combo_algo)
.combobox()
.pos(350, btn_y)
.size(100, 200) .dark_mode(is_dark)
.build();
self.hwnd_force = ControlBuilder::new(parent, self.ids.chk_force)
.checkbox()
.text("Force")
.pos(360, btn_y)
.size(60, btn_h)
.dark_mode(is_dark)
.build();
self.hwnd_process = ControlBuilder::new(parent, self.ids.btn_process)
.button()
.text("Process All")
.pos(430, btn_y)
.size(100, btn_h)
.dark_mode(is_dark)
.build();
apply_accent_button_theme(self.hwnd_process, is_dark);
self.hwnd_cancel = ControlBuilder::new(parent, self.ids.btn_cancel)
.button()
.text("Cancel")
.pos(540, btn_y)
.size(80, btn_h)
.dark_mode(is_dark)
.build();
Ok(())
}}
fn hwnd(&self) -> Option<HWND> {
Some(self.hwnd_process)
}
unsafe fn on_resize(&mut self, parent_rect: &RECT) {
unsafe {
let height = parent_rect.bottom - parent_rect.top;
let padding = 10;
let btn_height = 30;
let lbl_height = 16;
let lbl_btn_gap = 2;
let btn_y = height - btn_height - padding;
let lbl_y = btn_y - lbl_height - lbl_btn_gap;
SetWindowPos(
self.hwnd_lbl_input,
std::ptr::null_mut(),
padding,
lbl_y,
175,
lbl_height,
SWP_NOZORDER,
);
SetWindowPos(
self.hwnd_files,
std::ptr::null_mut(),
padding,
btn_y,
55,
btn_height,
SWP_NOZORDER,
);
SetWindowPos(
self.hwnd_folder,
std::ptr::null_mut(),
padding + 60,
btn_y,
55,
btn_height,
SWP_NOZORDER,
);
SetWindowPos(
self.hwnd_remove,
std::ptr::null_mut(),
padding + 120,
btn_y,
65,
btn_height,
SWP_NOZORDER,
);
SetWindowPos(
self.hwnd_lbl_action_mode,
std::ptr::null_mut(),
padding + 190,
lbl_y,
100,
lbl_height,
SWP_NOZORDER,
);
SetWindowPos(
self.hwnd_action_mode,
std::ptr::null_mut(),
padding + 190,
btn_y,
100,
btn_height,
SWP_NOZORDER,
);
SetWindowPos(
self.hwnd_lbl_algo,
std::ptr::null_mut(),
padding + 295,
lbl_y,
100,
lbl_height,
SWP_NOZORDER,
);
SetWindowPos(
self.hwnd_combo,
std::ptr::null_mut(),
padding + 295,
btn_y,
100,
btn_height,
SWP_NOZORDER,
);
SetWindowPos(
self.hwnd_force,
std::ptr::null_mut(),
padding + 400,
btn_y,
65,
btn_height,
SWP_NOZORDER,
);
SetWindowPos(
self.hwnd_process,
std::ptr::null_mut(),
padding + 470,
btn_y,
110, btn_height,
SWP_NOZORDER,
);
SetWindowPos(
self.hwnd_cancel,
std::ptr::null_mut(),
padding + 590, btn_y,
70,
btn_height,
SWP_NOZORDER,
);
}
}
unsafe fn on_theme_change(&mut self, is_dark: bool) {
unsafe {
apply_button_theme(self.hwnd_files, is_dark);
apply_button_theme(self.hwnd_folder, is_dark);
apply_button_theme(self.hwnd_remove, is_dark);
apply_accent_button_theme(self.hwnd_process, is_dark);
apply_button_theme(self.hwnd_cancel, is_dark);
apply_button_theme(self.hwnd_force, is_dark);
apply_combobox_theme(self.hwnd_action_mode, is_dark);
apply_combobox_theme(self.hwnd_combo, is_dark);
crate::ui::theme::apply_theme(self.hwnd_lbl_input, crate::ui::theme::ControlType::GroupBox, is_dark);
crate::ui::theme::apply_theme(self.hwnd_lbl_action_mode, crate::ui::theme::ControlType::GroupBox, is_dark);
crate::ui::theme::apply_theme(self.hwnd_lbl_algo, crate::ui::theme::ControlType::GroupBox, is_dark);
}
}
}