2022-10-01 16:58:22 +00:00
// Gupax - GUI Uniting P2Pool And XMRig
//
2023-02-26 16:45:58 +00:00
// Copyright (c) 2022-2023 hinto-janai
2022-10-01 16:58:22 +00:00
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
2022-11-16 19:40:25 +00:00
use crate ::State ;
2022-11-14 02:56:25 +00:00
use egui ::{
2022-12-07 02:13:37 +00:00
TextEdit ,
2022-12-23 02:57:49 +00:00
TextStyle ,
2022-11-14 02:56:25 +00:00
TextStyle ::Monospace ,
2022-11-23 04:21:46 +00:00
Checkbox , ProgressBar , Spinner , Button , Label , Slider ,
SelectableLabel ,
2022-11-24 04:03:56 +00:00
RichText ,
2022-11-23 04:21:46 +00:00
Vec2 ,
2022-11-14 02:56:25 +00:00
} ;
2022-11-25 01:28:13 +00:00
use crate ::{
constants ::* ,
update ::* ,
2022-11-27 03:26:32 +00:00
ErrorState ,
2022-11-27 20:11:00 +00:00
Restart ,
2022-12-06 03:33:35 +00:00
Tab ,
2022-12-29 03:03:45 +00:00
macros ::* ,
2022-11-25 01:28:13 +00:00
} ;
2022-11-20 18:31:00 +00:00
use std ::{
thread ,
sync ::{ Arc , Mutex } ,
2022-11-24 04:03:56 +00:00
path ::Path ,
2022-11-20 18:31:00 +00:00
} ;
2022-10-27 03:15:56 +00:00
use log ::* ;
2022-11-23 04:21:46 +00:00
use serde ::{ Serialize , Deserialize } ;
2022-10-01 16:58:22 +00:00
2022-11-17 18:03:45 +00:00
//---------------------------------------------------------------------------------------------------- FileWindow
// Struct for writing/reading the path state.
// The opened file picker is started in a new
// thread so main() needs to be in sync.
pub struct FileWindow {
thread : bool , // Is there already a FileWindow thread?
picked_p2pool : bool , // Did the user pick a path for p2pool?
picked_xmrig : bool , // Did the user pick a path for xmrig?
p2pool_path : String , // The picked p2pool path
xmrig_path : String , // The picked p2pool path
}
impl FileWindow {
pub fn new ( ) -> Arc < Mutex < Self > > {
2022-12-29 03:03:45 +00:00
arc_mut! ( Self {
2022-11-17 18:03:45 +00:00
thread : false ,
picked_p2pool : false ,
picked_xmrig : false ,
p2pool_path : String ::new ( ) ,
xmrig_path : String ::new ( ) ,
2022-12-29 03:03:45 +00:00
} )
2022-11-17 18:03:45 +00:00
}
}
2022-11-22 00:57:36 +00:00
#[ derive(Debug,Clone) ]
pub enum FileType {
P2pool ,
Xmrig ,
}
2022-11-23 04:21:46 +00:00
//---------------------------------------------------------------------------------------------------- Ratio Lock
// Enum for the lock ratio in the advanced tab.
#[ derive(Clone,Copy,Eq,PartialEq,Debug,Deserialize,Serialize) ]
pub enum Ratio {
Width ,
Height ,
None ,
}
2022-11-17 18:03:45 +00:00
//---------------------------------------------------------------------------------------------------- Gupax
2022-12-27 17:58:46 +00:00
impl crate ::disk ::Gupax {
2023-03-17 20:12:06 +00:00
#[ inline(always) ]
2022-12-14 03:41:05 +00:00
pub fn show ( & mut self , og : & Arc < Mutex < State > > , state_path : & Path , update : & Arc < Mutex < Update > > , file_window : & Arc < Mutex < FileWindow > > , error_state : & mut ErrorState , restart : & Arc < Mutex < Restart > > , width : f32 , height : f32 , frame : & mut eframe ::Frame , _ctx : & egui ::Context , ui : & mut egui ::Ui ) {
2022-10-27 03:15:56 +00:00
// Update button + Progress bar
2022-12-12 19:34:17 +00:00
debug! ( " Gupax Tab | Rendering [Update] button + progress bar " ) ;
2022-10-27 03:15:56 +00:00
ui . group ( | ui | {
2022-12-23 02:57:49 +00:00
let button = if self . simple { height / 5.0 } else { height / 15.0 } ;
2022-12-06 03:33:35 +00:00
let height = if self . simple { height / 5.0 } else { height / 10.0 } ;
2022-10-27 03:15:56 +00:00
let width = width - SPACE ;
2022-12-29 03:03:45 +00:00
let updating = * lock2! ( update , updating ) ;
2022-10-27 03:15:56 +00:00
ui . vertical ( | ui | {
2022-12-18 02:42:30 +00:00
// If [Gupax] is being built for a Linux distro,
// disable built-in updating completely.
#[ cfg(feature = " distro " ) ]
ui . set_enabled ( false ) ;
#[ cfg(feature = " distro " ) ]
2022-12-23 02:57:49 +00:00
ui . add_sized ( [ width , button ] , Button ::new ( " Updates are disabled " ) ) . on_disabled_hover_text ( DISTRO_NO_UPDATE ) ;
2022-12-18 02:42:30 +00:00
#[ cfg(not(feature = " distro " )) ]
2022-10-27 15:52:18 +00:00
ui . set_enabled ( ! updating ) ;
2022-12-18 02:42:30 +00:00
#[ cfg(not(feature = " distro " )) ]
2022-12-23 02:57:49 +00:00
if ui . add_sized ( [ width , button ] , Button ::new ( " Check for updates " ) ) . on_hover_text ( GUPAX_UPDATE ) . clicked ( ) {
2022-12-14 03:41:05 +00:00
Update ::spawn_thread ( og , self , state_path , update , error_state , restart ) ;
2022-10-27 03:15:56 +00:00
}
} ) ;
ui . vertical ( | ui | {
2022-10-27 15:52:18 +00:00
ui . set_enabled ( updating ) ;
2022-12-29 03:03:45 +00:00
let prog = * lock2! ( update , prog ) ;
let msg = format! ( " {} \n {} {} " , * lock2! ( update , msg ) , prog , " % " ) ;
2022-11-23 04:21:46 +00:00
ui . add_sized ( [ width , height * 1.4 ] , Label ::new ( RichText ::text_style ( RichText ::new ( msg ) , Monospace ) ) ) ;
2022-10-27 03:15:56 +00:00
let height = height / 2.0 ;
2022-10-28 19:45:13 +00:00
if updating {
2022-11-23 04:21:46 +00:00
ui . add_sized ( [ width , height ] , Spinner ::new ( ) . size ( height ) ) ;
2022-10-28 19:45:13 +00:00
} else {
2022-11-23 04:21:46 +00:00
ui . add_sized ( [ width , height ] , Label ::new ( " ... " ) ) ;
2022-10-28 19:45:13 +00:00
}
2022-12-29 03:03:45 +00:00
ui . add_sized ( [ width , height ] , ProgressBar ::new ( lock2! ( update , prog ) . round ( ) / 100.0 ) ) ;
2022-10-27 03:15:56 +00:00
} ) ;
} ) ;
2022-10-01 16:58:22 +00:00
2022-12-12 19:34:17 +00:00
debug! ( " Gupax Tab | Rendering bool buttons " ) ;
2022-10-01 16:58:22 +00:00
ui . horizontal ( | ui | {
ui . group ( | ui | {
2022-12-11 04:06:24 +00:00
let width = ( width - SPACE * 12.0 ) / 6.0 ;
2022-12-23 02:57:49 +00:00
let height = if self . simple { height / 10.0 } else { height / 15.0 } ;
2022-12-11 04:06:24 +00:00
ui . style_mut ( ) . override_text_style = Some ( egui ::TextStyle ::Small ) ;
2022-11-23 04:21:46 +00:00
ui . add_sized ( [ width , height ] , Checkbox ::new ( & mut self . update_via_tor , " Update via Tor " ) ) . on_hover_text ( GUPAX_UPDATE_VIA_TOR ) ;
2022-10-27 03:15:56 +00:00
ui . separator ( ) ;
2022-12-11 04:06:24 +00:00
ui . add_sized ( [ width , height ] , Checkbox ::new ( & mut self . auto_update , " Auto-Update " ) ) . on_hover_text ( GUPAX_AUTO_UPDATE ) ;
ui . separator ( ) ;
ui . add_sized ( [ width , height ] , Checkbox ::new ( & mut self . auto_p2pool , " Auto-P2Pool " ) ) . on_hover_text ( GUPAX_AUTO_P2POOL ) ;
ui . separator ( ) ;
ui . add_sized ( [ width , height ] , Checkbox ::new ( & mut self . auto_xmrig , " Auto-XMRig " ) ) . on_hover_text ( GUPAX_AUTO_XMRIG ) ;
ui . separator ( ) ;
2022-11-23 04:21:46 +00:00
ui . add_sized ( [ width , height ] , Checkbox ::new ( & mut self . ask_before_quit , " Ask before quit " ) ) . on_hover_text ( GUPAX_ASK_BEFORE_QUIT ) ;
2022-10-27 03:15:56 +00:00
ui . separator ( ) ;
2022-11-23 04:21:46 +00:00
ui . add_sized ( [ width , height ] , Checkbox ::new ( & mut self . save_before_quit , " Save before quit " ) ) . on_hover_text ( GUPAX_SAVE_BEFORE_QUIT ) ;
2022-10-01 16:58:22 +00:00
} ) ;
} ) ;
2022-11-23 04:21:46 +00:00
if self . simple { return }
2022-12-12 19:34:17 +00:00
debug! ( " Gupax Tab | Rendering P2Pool/XMRig path selection " ) ;
2022-11-23 04:21:46 +00:00
// P2Pool/XMRig binary path selection
2022-11-14 02:56:25 +00:00
ui . style_mut ( ) . override_text_style = Some ( Monospace ) ;
2022-12-07 02:13:37 +00:00
let height = height / 28.0 ;
2022-11-14 02:56:25 +00:00
let text_edit = ( ui . available_width ( ) / 10.0 ) - SPACE ;
2022-12-23 02:57:49 +00:00
ui . group ( | ui | {
ui . add_sized ( [ ui . available_width ( ) , height / 2.0 ] , Label ::new ( RichText ::new ( " P2Pool/XMRig PATHs " ) . underline ( ) . color ( LIGHT_GRAY ) . text_style ( TextStyle ::Monospace ) ) ) . on_hover_text ( " Gupax is online " ) ;
ui . separator ( ) ;
2022-10-01 16:58:22 +00:00
ui . horizontal ( | ui | {
2022-11-14 02:56:25 +00:00
if self . p2pool_path . is_empty ( ) {
2022-12-16 14:47:16 +00:00
ui . add_sized ( [ text_edit , height ] , Label ::new ( RichText ::new ( " P2Pool Binary Path ➖ " ) . color ( LIGHT_GRAY ) ) ) . on_hover_text ( P2POOL_PATH_EMPTY ) ;
2022-12-19 01:17:32 +00:00
} else if ! Self ::path_is_file ( & self . p2pool_path ) {
ui . add_sized ( [ text_edit , height ] , Label ::new ( RichText ::new ( " P2Pool Binary Path ❌ " ) . color ( RED ) ) ) . on_hover_text ( P2POOL_PATH_NOT_FILE ) ;
} else if ! crate ::update ::check_p2pool_path ( & self . p2pool_path ) {
ui . add_sized ( [ text_edit , height ] , Label ::new ( RichText ::new ( " P2Pool Binary Path ❌ " ) . color ( RED ) ) ) . on_hover_text ( P2POOL_PATH_NOT_VALID ) ;
2022-11-14 02:56:25 +00:00
} else {
2022-12-19 01:17:32 +00:00
ui . add_sized ( [ text_edit , height ] , Label ::new ( RichText ::new ( " P2Pool Binary Path ✔ " ) . color ( GREEN ) ) ) . on_hover_text ( P2POOL_PATH_OK ) ;
2022-11-14 02:56:25 +00:00
}
2022-10-28 19:45:13 +00:00
ui . spacing_mut ( ) . text_edit_width = ui . available_width ( ) - SPACE ;
2022-12-29 03:03:45 +00:00
ui . set_enabled ( ! lock! ( file_window ) . thread ) ;
2022-11-18 03:45:57 +00:00
if ui . button ( " Open " ) . on_hover_text ( GUPAX_SELECT ) . clicked ( ) {
2022-11-24 04:03:56 +00:00
Self ::spawn_file_window_thread ( file_window , FileType ::P2pool ) ;
2022-11-17 18:03:45 +00:00
}
2022-12-23 02:57:49 +00:00
ui . add_sized ( [ ui . available_width ( ) , height ] , TextEdit ::singleline ( & mut self . p2pool_path ) ) . on_hover_text ( GUPAX_PATH_P2POOL ) ;
2022-10-01 16:58:22 +00:00
} ) ;
ui . horizontal ( | ui | {
2022-11-14 02:56:25 +00:00
if self . xmrig_path . is_empty ( ) {
2022-12-16 14:47:16 +00:00
ui . add_sized ( [ text_edit , height ] , Label ::new ( RichText ::new ( " XMRig Binary Path ➖ " ) . color ( LIGHT_GRAY ) ) ) . on_hover_text ( XMRIG_PATH_EMPTY ) ;
2022-12-19 01:17:32 +00:00
} else if ! Self ::path_is_file ( & self . xmrig_path ) {
ui . add_sized ( [ text_edit , height ] , Label ::new ( RichText ::new ( " XMRig Binary Path ❌ " ) . color ( RED ) ) ) . on_hover_text ( XMRIG_PATH_NOT_FILE ) ;
} else if ! crate ::update ::check_xmrig_path ( & self . xmrig_path ) {
ui . add_sized ( [ text_edit , height ] , Label ::new ( RichText ::new ( " XMRig Binary Path ❌ " ) . color ( RED ) ) ) . on_hover_text ( XMRIG_PATH_NOT_VALID ) ;
2022-11-14 02:56:25 +00:00
} else {
2022-12-19 01:17:32 +00:00
ui . add_sized ( [ text_edit , height ] , Label ::new ( RichText ::new ( " XMRig Binary Path ✔ " ) . color ( GREEN ) ) ) . on_hover_text ( XMRIG_PATH_OK ) ;
2022-11-14 02:56:25 +00:00
}
2022-10-28 19:45:13 +00:00
ui . spacing_mut ( ) . text_edit_width = ui . available_width ( ) - SPACE ;
2022-12-29 03:03:45 +00:00
ui . set_enabled ( ! lock! ( file_window ) . thread ) ;
2022-11-18 19:25:13 +00:00
if ui . button ( " Open " ) . on_hover_text ( GUPAX_SELECT ) . clicked ( ) {
2022-11-24 04:03:56 +00:00
Self ::spawn_file_window_thread ( file_window , FileType ::Xmrig ) ;
2022-11-17 18:03:45 +00:00
}
2022-12-23 02:57:49 +00:00
ui . add_sized ( [ ui . available_width ( ) , height ] , TextEdit ::singleline ( & mut self . xmrig_path ) ) . on_hover_text ( GUPAX_PATH_XMRIG ) ;
} ) ;
2022-10-01 16:58:22 +00:00
} ) ;
2022-12-29 03:03:45 +00:00
let mut guard = lock! ( file_window ) ;
2022-11-17 18:03:45 +00:00
if guard . picked_p2pool { self . p2pool_path = guard . p2pool_path . clone ( ) ; guard . picked_p2pool = false ; }
if guard . picked_xmrig { self . xmrig_path = guard . xmrig_path . clone ( ) ; guard . picked_xmrig = false ; }
drop ( guard ) ;
2022-11-23 04:21:46 +00:00
2022-12-23 02:57:49 +00:00
let height = ui . available_height ( ) / 6.0 ;
// Saved [Tab]
debug! ( " Gupax Tab | Rendering [Tab] selector " ) ;
ui . group ( | ui | {
let width = ( width / 5.0 ) - ( SPACE * 1.93 ) ;
ui . add_sized ( [ ui . available_width ( ) , height / 2.0 ] , Label ::new ( RichText ::new ( " Default Tab " ) . underline ( ) . color ( LIGHT_GRAY ) . text_style ( TextStyle ::Monospace ) ) ) . on_hover_text ( GUPAX_TAB ) ;
ui . separator ( ) ;
ui . horizontal ( | ui | {
if ui . add_sized ( [ width , height ] , SelectableLabel ::new ( self . tab = = Tab ::About , " About " ) ) . on_hover_text ( GUPAX_TAB_ABOUT ) . clicked ( ) { self . tab = Tab ::About ; }
ui . separator ( ) ;
if ui . add_sized ( [ width , height ] , SelectableLabel ::new ( self . tab = = Tab ::Status , " Status " ) ) . on_hover_text ( GUPAX_TAB_STATUS ) . clicked ( ) { self . tab = Tab ::Status ; }
ui . separator ( ) ;
if ui . add_sized ( [ width , height ] , SelectableLabel ::new ( self . tab = = Tab ::Gupax , " Gupax " ) ) . on_hover_text ( GUPAX_TAB_GUPAX ) . clicked ( ) { self . tab = Tab ::Gupax ; }
ui . separator ( ) ;
if ui . add_sized ( [ width , height ] , SelectableLabel ::new ( self . tab = = Tab ::P2pool , " P2Pool " ) ) . on_hover_text ( GUPAX_TAB_P2POOL ) . clicked ( ) { self . tab = Tab ::P2pool ; }
ui . separator ( ) ;
if ui . add_sized ( [ width , height ] , SelectableLabel ::new ( self . tab = = Tab ::Xmrig , " XMRig " ) ) . on_hover_text ( GUPAX_TAB_XMRIG ) . clicked ( ) { self . tab = Tab ::Xmrig ; }
} ) } ) ;
2022-11-23 04:21:46 +00:00
// Gupax App resolution sliders
2022-12-12 19:34:17 +00:00
debug! ( " Gupax Tab | Rendering resolution sliders " ) ;
2022-12-23 02:57:49 +00:00
ui . group ( | ui | {
ui . add_sized ( [ ui . available_width ( ) , height / 2.0 ] , Label ::new ( RichText ::new ( " Width/Height Adjust " ) . underline ( ) . color ( LIGHT_GRAY ) . text_style ( TextStyle ::Monospace ) ) ) . on_hover_text ( GUPAX_ADJUST ) ;
ui . separator ( ) ;
2022-11-23 04:21:46 +00:00
ui . vertical ( | ui | {
let width = width / 10.0 ;
ui . spacing_mut ( ) . icon_width = width / 25.0 ;
ui . spacing_mut ( ) . slider_width = width * 7.6 ;
match self . ratio {
Ratio ::None = > ( ) ,
Ratio ::Width = > {
let width = self . selected_width as f64 ;
2022-12-07 03:01:36 +00:00
let height = ( width / 1.333 ) . round ( ) ;
2022-11-23 04:21:46 +00:00
self . selected_height = height as u16 ;
} ,
Ratio ::Height = > {
let height = self . selected_height as f64 ;
2022-12-07 03:01:36 +00:00
let width = ( height * 1.333 ) . round ( ) ;
2022-11-23 04:21:46 +00:00
self . selected_width = width as u16 ;
} ,
}
2022-12-23 02:57:49 +00:00
let height = height / 2.5 ;
2022-11-23 04:21:46 +00:00
ui . horizontal ( | ui | {
ui . set_enabled ( self . ratio ! = Ratio ::Height ) ;
ui . add_sized ( [ width , height ] , Label ::new ( format! ( " Width [ {} - {} ]: " , APP_MIN_WIDTH as u16 , APP_MAX_WIDTH as u16 ) ) ) ;
ui . add_sized ( [ width , height ] , Slider ::new ( & mut self . selected_width , APP_MIN_WIDTH as u16 ..= APP_MAX_WIDTH as u16 ) ) . on_hover_text ( GUPAX_WIDTH ) ;
} ) ;
ui . horizontal ( | ui | {
ui . set_enabled ( self . ratio ! = Ratio ::Width ) ;
ui . add_sized ( [ width , height ] , Label ::new ( format! ( " Height [ {} - {} ]: " , APP_MIN_HEIGHT as u16 , APP_MAX_HEIGHT as u16 ) ) ) ;
ui . add_sized ( [ width , height ] , Slider ::new ( & mut self . selected_height , APP_MIN_HEIGHT as u16 ..= APP_MAX_HEIGHT as u16 ) ) . on_hover_text ( GUPAX_HEIGHT ) ;
} ) ;
} ) ;
ui . style_mut ( ) . override_text_style = Some ( egui ::TextStyle ::Button ) ;
2022-12-23 02:57:49 +00:00
ui . separator ( ) ;
2022-12-06 03:33:35 +00:00
// Width/Height locks
2022-11-23 04:21:46 +00:00
ui . horizontal ( | ui | {
2022-12-06 03:33:35 +00:00
use Ratio ::* ;
let width = ( width / 4.0 ) - ( SPACE * 1.5 ) ;
if ui . add_sized ( [ width , height ] , SelectableLabel ::new ( self . ratio = = Width , " Lock to width " ) ) . on_hover_text ( GUPAX_LOCK_WIDTH ) . clicked ( ) { self . ratio = Width ; }
2022-11-23 04:21:46 +00:00
ui . separator ( ) ;
2022-12-06 03:33:35 +00:00
if ui . add_sized ( [ width , height ] , SelectableLabel ::new ( self . ratio = = Height , " Lock to height " ) ) . on_hover_text ( GUPAX_LOCK_HEIGHT ) . clicked ( ) { self . ratio = Height ; }
2022-11-23 04:21:46 +00:00
ui . separator ( ) ;
2022-12-06 03:33:35 +00:00
if ui . add_sized ( [ width , height ] , SelectableLabel ::new ( self . ratio = = None , " No lock " ) ) . on_hover_text ( GUPAX_NO_LOCK ) . clicked ( ) { self . ratio = None ; }
2022-11-23 04:21:46 +00:00
if ui . add_sized ( [ width , height ] , Button ::new ( " Set " ) ) . on_hover_text ( GUPAX_SET ) . clicked ( ) {
frame . set_window_size ( Vec2 ::new ( self . selected_width as f32 , self . selected_height as f32 ) ) ;
}
} ) } ) ;
2022-10-01 16:58:22 +00:00
}
2022-11-22 00:57:36 +00:00
2022-12-16 14:47:16 +00:00
// Checks if a path is a valid path to a file.
pub fn path_is_file ( path : & str ) -> bool {
2022-12-09 01:24:37 +00:00
let path = path . to_string ( ) ;
match crate ::disk ::into_absolute_path ( path ) {
2022-12-14 03:41:05 +00:00
Ok ( path ) = > path . is_file ( ) ,
2022-12-09 01:24:37 +00:00
_ = > false ,
}
}
2022-11-22 00:57:36 +00:00
fn spawn_file_window_thread ( file_window : & Arc < Mutex < FileWindow > > , file_type : FileType ) {
use FileType ::* ;
let name = match file_type {
P2pool = > " P2Pool " ,
Xmrig = > " XMRig " ,
} ;
let file_window = file_window . clone ( ) ;
2022-12-29 03:03:45 +00:00
lock! ( file_window ) . thread = true ;
2022-11-22 00:57:36 +00:00
thread ::spawn ( move | | {
match rfd ::FileDialog ::new ( ) . set_title ( & format! ( " Select {} Binary for Gupax " , name ) ) . pick_file ( ) {
Some ( path ) = > {
info! ( " Gupax | Path selected for {} ... {} " , name , path . display ( ) ) ;
match file_type {
2022-12-29 03:03:45 +00:00
P2pool = > { lock! ( file_window ) . p2pool_path = path . display ( ) . to_string ( ) ; lock! ( file_window ) . picked_p2pool = true ; } ,
Xmrig = > { lock! ( file_window ) . xmrig_path = path . display ( ) . to_string ( ) ; lock! ( file_window ) . picked_xmrig = true ; } ,
2022-11-22 00:57:36 +00:00
} ;
} ,
None = > info! ( " Gupax | No path selected for {} " , name ) ,
} ;
2022-12-29 03:03:45 +00:00
lock! ( file_window ) . thread = false ;
2022-11-22 00:57:36 +00:00
} ) ;
}
2022-10-01 16:58:22 +00:00
}