22// SPDX-License-Identifier: GPL-3.0-only
33
44use crate :: {
5- AppWindow , Config , Game , HomebrewApp , Notification , OscApp , QueuedConversion , game,
6- homebrew_app, osc,
5+ AppWindow , Config , Game , HomebrewApp , Notification , OscApp , QueuedConversion , SortBy , game,
6+ homebrew_app, mirrored:: Mirrored , osc,
7+ } ;
8+ use slint:: { FilterModel , ModelRc , SharedString , SortModel , ToSharedString , VecModel } ;
9+ use std:: {
10+ cell:: { Ref , RefCell } ,
11+ cmp:: Ordering ,
12+ path:: PathBuf ,
13+ rc:: Rc ,
714} ;
8- use slint:: { FilterModel , ModelRc , SharedString , SortModel , VecModel } ;
9- use std:: { cell:: RefCell , cmp:: Ordering , rc:: Rc } ;
1015
1116type SortedModel < T > = SortModel < Rc < VecModel < T > > , Box < dyn Fn ( & T , & T ) -> Ordering > > ;
1217type FilteredModel < T > = FilterModel < Rc < SortedModel < T > > , Box < dyn Fn ( & T ) -> bool > > ;
1318type JustFilteredModel < T > = FilterModel < Rc < VecModel < T > > , Box < dyn Fn ( & T ) -> bool > > ;
1419
1520#[ derive( Clone ) ]
1621pub struct AppModel {
17- config : Rc < RefCell < Config > > ,
22+ config : Rc < Mirrored < Config , AppWindow > > ,
23+ status : Rc < Mirrored < SharedString , AppWindow > > ,
24+ crc32_status : Rc < Mirrored < SharedString , AppWindow > > ,
1825
1926 games : Rc < VecModel < Game > > ,
2027 homebrew_apps : Rc < VecModel < HomebrewApp > > ,
@@ -39,7 +46,17 @@ pub struct AppModel {
3946
4047impl AppModel {
4148 pub fn new ( config : Config , app : & AppWindow ) -> Self {
42- let config = Rc :: new ( RefCell :: new ( config) ) ;
49+ let config = Rc :: new ( Mirrored :: new ( config, app, AppWindow :: set_config) ) ;
50+ let status = Rc :: new ( Mirrored :: new (
51+ SharedString :: new ( ) ,
52+ app,
53+ AppWindow :: set_status,
54+ ) ) ;
55+ let crc32_status = Rc :: new ( Mirrored :: new (
56+ SharedString :: new ( ) ,
57+ app,
58+ AppWindow :: set_crc32_status,
59+ ) ) ;
4360
4461 let games = Rc :: new ( VecModel :: from ( Vec :: new ( ) ) ) ;
4562 let homebrew_apps = Rc :: new ( VecModel :: from ( Vec :: new ( ) ) ) ;
@@ -83,6 +100,8 @@ impl AppModel {
83100
84101 Self {
85102 config,
103+ status,
104+ crc32_status,
86105 games,
87106 homebrew_apps,
88107 osc_apps,
@@ -100,26 +119,53 @@ impl AppModel {
100119 }
101120 }
102121
103- pub fn config ( & self ) -> Config {
104- self . config . borrow ( ) . clone ( )
122+ pub fn borrow_config ( & self ) -> Ref < ' _ , Config > {
123+ self . config . borrow ( )
105124 }
106125
107- pub fn set_config ( & self , config : Config ) {
108- let old_config = self . config . replace ( config) ;
109- let config = self . config . borrow ( ) ;
126+ pub fn set_mount_point ( & self , mount_point : PathBuf ) {
127+ self . config . edit ( |config| {
128+ config. contents . mount_point = mount_point. to_string_lossy ( ) . to_shared_string ( ) ;
129+ } ) ;
110130
111- if old_config. contents . sort_by != config. contents . sort_by {
112- self . sorted_games . reset ( ) ;
113- self . sorted_homebrew_apps . reset ( ) ;
131+ if let Err ( e) = self . config . borrow ( ) . write ( ) {
132+ self . add_notification ( e. into ( ) ) ;
114133 }
134+ }
115135
116- if old_config. contents . show_wii != config. contents . show_wii
117- || old_config. contents . show_gc != config. contents . show_gc
118- {
119- self . filtered_games . reset ( ) ;
136+ pub fn set_sort_by ( & self , sort_by : SortBy ) {
137+ self . config . edit ( |config| {
138+ config. contents . sort_by = sort_by;
139+ } ) ;
140+
141+ self . sorted_games . reset ( ) ;
142+ self . sorted_homebrew_apps . reset ( ) ;
143+
144+ if let Err ( e) = self . config . borrow ( ) . write ( ) {
145+ self . add_notification ( e. into ( ) ) ;
120146 }
147+ }
148+
149+ pub fn set_show_wii ( & self , show_wii : bool ) {
150+ self . config . edit ( |config| {
151+ config. contents . show_wii = show_wii;
152+ } ) ;
153+
154+ self . filtered_games . reset ( ) ;
155+
156+ if let Err ( e) = self . config . borrow ( ) . write ( ) {
157+ self . add_notification ( e. into ( ) ) ;
158+ }
159+ }
160+
161+ pub fn set_show_gc ( & self , show_gc : bool ) {
162+ self . config . edit ( |config| {
163+ config. contents . show_gc = show_gc;
164+ } ) ;
165+
166+ self . filtered_games . reset ( ) ;
121167
122- if let Err ( e) = config. write ( ) {
168+ if let Err ( e) = self . config . borrow ( ) . write ( ) {
123169 self . add_notification ( e. into ( ) ) ;
124170 }
125171 }
0 commit comments