Splash in separate file. Events refactored

This commit is contained in:
Vladimir Borisov 2021-07-27 15:02:39 +03:00
parent 4054020d40
commit 2dc3338b33
3 changed files with 42 additions and 19 deletions

View file

@ -17,6 +17,7 @@ use crate::stremio_app::ipc::{RPCRequest, RPCResponse, RPCResponseData, RPCRespo
use crate::stremio_app::stremio_player::Player; use crate::stremio_app::stremio_player::Player;
use crate::stremio_app::stremio_wevbiew::WebView; use crate::stremio_app::stremio_wevbiew::WebView;
use crate::stremio_app::systray::SystemTray; use crate::stremio_app::systray::SystemTray;
use crate::stremio_app::splash::SplashImage;
// https://doc.qt.io/qt-5/qt.html#WindowState-enum // https://doc.qt.io/qt-5/qt.html#WindowState-enum
bitflags! { bitflags! {
@ -68,23 +69,19 @@ pub struct MainWindow {
#[nwg_events( OnWindowClose: [Self::on_quit(SELF, EVT_DATA)], OnInit: [Self::on_init], OnPaint: [Self::on_paint], OnMinMaxInfo: [Self::on_min_max(SELF, EVT_DATA)], OnWindowMaximize: [Self::transmit_window_state_change], OnWindowMinimize: [Self::transmit_window_state_change] )] #[nwg_events( OnWindowClose: [Self::on_quit(SELF, EVT_DATA)], OnInit: [Self::on_init], OnPaint: [Self::on_paint], OnMinMaxInfo: [Self::on_min_max(SELF, EVT_DATA)], OnWindowMaximize: [Self::transmit_window_state_change], OnWindowMinimize: [Self::transmit_window_state_change] )]
pub window: nwg::Window, pub window: nwg::Window,
#[nwg_partial(parent: window)] #[nwg_partial(parent: window)]
#[nwg_events((tray, MousePressLeftUp): [Self::on_show_hide], (tray_exit, OnMenuItemSelected): [Self::on_quit_notice], (tray_show_hide, OnMenuItemSelected): [Self::on_show_hide], (tray_topmost, OnMenuItemSelected): [Self::on_toggle_topmost]) ] #[nwg_events((tray, MousePressLeftUp): [Self::on_show_hide], (tray_exit, OnMenuItemSelected): [nwg::stop_thread_dispatch()], (tray_show_hide, OnMenuItemSelected): [Self::on_show_hide], (tray_topmost, OnMenuItemSelected): [Self::on_toggle_topmost]) ]
pub tray: SystemTray, pub tray: SystemTray,
#[nwg_partial(parent: window)] #[nwg_partial(parent: window)]
pub webview: WebView, pub webview: WebView,
#[nwg_partial(parent: window)] #[nwg_partial(parent: window)]
pub player: Player, pub player: Player,
#[nwg_resource(size: Some((300,300)), source_embed: Some(&data.embed), source_embed_str: Some("SPLASHIMAGE"))] #[nwg_partial(parent: window)]
pub splash_image: nwg::Bitmap, pub splash_screen: SplashImage,
#[nwg_control(parent: window, background_color: Some(Self::BG_COLOR))]
pub splash_frame: nwg::ImageFrame,
#[nwg_control(parent: splash_frame, background_color: Some(Self::BG_COLOR), bitmap: Some(&data.splash_image))]
pub splash: nwg::ImageFrame,
#[nwg_control] #[nwg_control]
#[nwg_events(OnNotice: [Self::on_toggle_fullscreen_notice] )] #[nwg_events(OnNotice: [Self::on_toggle_fullscreen_notice] )]
pub toggle_fullscreen_notice: nwg::Notice, pub toggle_fullscreen_notice: nwg::Notice,
#[nwg_control] #[nwg_control]
#[nwg_events(OnNotice: [Self::on_quit_notice] )] #[nwg_events(OnNotice: [nwg::stop_thread_dispatch()] )]
pub quit_notice: nwg::Notice, pub quit_notice: nwg::Notice,
#[nwg_control] #[nwg_control]
#[nwg_events(OnNotice: [Self::on_hide_splash_notice] )] #[nwg_events(OnNotice: [Self::on_hide_splash_notice] )]
@ -92,7 +89,6 @@ pub struct MainWindow {
} }
impl MainWindow { impl MainWindow {
const BG_COLOR: [u8; 3] = [27, 17, 38];
const MIN_WIDTH: i32 = 1000; const MIN_WIDTH: i32 = 1000;
const MIN_HEIGHT: i32 = 600; const MIN_HEIGHT: i32 = 600;
fn transmit_window_full_screen_change(&self, prevent_close: bool) { fn transmit_window_full_screen_change(&self, prevent_close: bool) {
@ -262,12 +258,8 @@ impl MainWindow {
data.set_min_size(Self::MIN_WIDTH, Self::MIN_HEIGHT); data.set_min_size(Self::MIN_WIDTH, Self::MIN_HEIGHT);
} }
fn on_paint(&self) { fn on_paint(&self) {
if self.splash_frame.visible() { if self.splash_screen.visible() {
let (w, h) = self.window.size(); self.splash_screen.resize(self.window.size());
let s = cmp::min(w, h);
self.splash_frame.set_size(w, h);
self.splash.set_size(s, s);
self.splash.set_position(w as i32 / 2 - s as i32 / 2, 0);
} else { } else {
self.transmit_window_state_change(); self.transmit_window_state_change();
} }
@ -333,11 +325,8 @@ impl MainWindow {
} }
self.transmit_window_full_screen_change(true); self.transmit_window_full_screen_change(true);
} }
fn on_quit_notice(&self) {
nwg::stop_thread_dispatch();
}
fn on_hide_splash_notice(&self) { fn on_hide_splash_notice(&self) {
self.splash_frame.set_visible(false); self.splash_screen.hide();
} }
fn on_toggle_topmost(&self) { fn on_toggle_topmost(&self) {
if let Some(hwnd) = self.window.handle.hwnd() { if let Some(hwnd) = self.window.handle.hwnd() {

View file

@ -10,4 +10,6 @@ pub mod ipc;
pub use ipc::{Channel, RPCRequest, RPCResponse, RPCResponseData, RPCResponseDataTransport}; pub use ipc::{Channel, RPCRequest, RPCResponse, RPCResponseData, RPCResponseDataTransport};
pub mod systray; pub mod systray;
pub use systray::SystemTray; pub use systray::SystemTray;
pub mod splash;
pub use splash::SplashImage;

32
src/stremio_app/splash.rs Normal file
View file

@ -0,0 +1,32 @@
use native_windows_derive::NwgPartial;
use native_windows_gui as nwg;
use std::cmp;
#[derive(Default, NwgPartial)]
pub struct SplashImage {
#[nwg_resource]
embed: nwg::EmbedResource,
#[nwg_resource(size: Some((300,300)), source_embed: Some(&data.embed), source_embed_str: Some("SPLASHIMAGE"))]
splash_image: nwg::Bitmap,
#[nwg_control(background_color: Some(Self::BG_COLOR))]
splash_frame: nwg::ImageFrame,
#[nwg_control(parent: splash_frame, background_color: Some(Self::BG_COLOR), bitmap: Some(&data.splash_image))]
splash: nwg::ImageFrame,
}
impl SplashImage {
const BG_COLOR: [u8; 3] = [27, 17, 38];
pub fn resize(&self, size:(u32, u32)) {
let (w, h) = size;
let s = cmp::min(w, h);
self.splash_frame.set_size(w, h);
self.splash.set_size(s, s);
self.splash.set_position(w as i32 / 2 - s as i32 / 2, 0);
}
pub fn visible(&self) -> bool {
self.splash_frame.visible()
}
pub fn hide(&self) {
self.splash_frame.set_visible(false);
}
}