Update the build environment and some architectural elements of the Kifu app #210

Merged
savanni merged 13 commits from kifu/flake into main 2024-02-28 04:42:58 +00:00
10 changed files with 97 additions and 33 deletions
Showing only changes of commit c366f0b93e - Show all commits

View File

@ -1,6 +1,5 @@
use crate::{ use crate::{
types::{AppState, Config, ConfigOption, DatabasePath, GameState, Player, Rank}, database::Database, types::{AppState, Config, ConfigOption, DatabasePath, GameState, Player, Rank}, ui::{configuration, home, playing_field, ConfigurationView, HomeView, PlayingFieldView}
ui::{configuration, home, playing_field, ConfigurationView, HomeView, PlayingFieldView},
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{ use std::{
@ -77,20 +76,21 @@ pub enum CoreResponse {
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct CoreApp { pub struct CoreApp {
config: Arc<RwLock<Config>>, // config: Arc<RwLock<Config>>,
state: Arc<RwLock<AppState>>, // state: Arc<RwLock<AppState>>,
database: Arc<RwLock<Option<Database>>>,
} }
impl CoreApp { impl CoreApp {
pub fn new(config_path: std::path::PathBuf) -> Self { pub fn new(config: Config) -> Self {
let config = Config::from_path(config_path).expect("configuration to open"); // let config = Config::from_path(config_path).expect("configuration to open");
let db_path: DatabasePath = config.get().unwrap(); // let state = Arc::new(RwLock::new(AppState::new(db_path)));
let state = Arc::new(RwLock::new(AppState::new(db_path)));
Self { Self {
config: Arc::new(RwLock::new(config)), // config: Arc::new(RwLock::new(config)),
state, // state,
database: Arc::new(RwLock::new(None)),
} }
} }
@ -98,14 +98,16 @@ impl CoreApp {
match request { match request {
CoreRequest::ChangeSetting(request) => match request { CoreRequest::ChangeSetting(request) => match request {
ChangeSettingRequest::LibraryPath(path) => { ChangeSettingRequest::LibraryPath(path) => {
let mut config = self.config.write().unwrap(); // let mut config = self.config.write().unwrap();
config.set(ConfigOption::DatabasePath(DatabasePath(PathBuf::from( // config.set(ConfigOption::DatabasePath(DatabasePath(PathBuf::from(
path, // path,
)))); // ))));
CoreResponse::UpdatedConfigurationView(configuration(&config)) // CoreResponse::UpdatedConfigurationView(configuration(&config))
unimplemented!()
} }
}, },
CoreRequest::CreateGame(create_request) => { CoreRequest::CreateGame(create_request) => {
/*
let mut app_state = self.state.write().unwrap(); let mut app_state = self.state.write().unwrap();
let white_player = { let white_player = {
match create_request.white_player { match create_request.white_player {
@ -124,24 +126,30 @@ impl CoreApp {
}); });
let game_state = app_state.game.as_ref().unwrap(); let game_state = app_state.game.as_ref().unwrap();
CoreResponse::PlayingFieldView(playing_field(game_state)) CoreResponse::PlayingFieldView(playing_field(game_state))
*/
unimplemented!()
} }
CoreRequest::Home => { CoreRequest::Home => {
CoreResponse::HomeView(home(self.state.read().unwrap().database.all_games())) // CoreResponse::HomeView(home(self.state.read().unwrap().database.all_games()))
unimplemented!()
} }
CoreRequest::OpenConfiguration => { CoreRequest::OpenConfiguration => {
CoreResponse::ConfigurationView(configuration(&self.config.read().unwrap())) // CoreResponse::ConfigurationView(configuration(&self.config.read().unwrap()))
unimplemented!()
} }
CoreRequest::PlayingField => { CoreRequest::PlayingField => {
let app_state = self.state.read().unwrap(); // let app_state = self.state.read().unwrap();
let game = app_state.game.as_ref().unwrap(); // let game = app_state.game.as_ref().unwrap();
CoreResponse::PlayingFieldView(playing_field(game)) // CoreResponse::PlayingFieldView(playing_field(game))
unimplemented!()
} }
CoreRequest::PlayStone(request) => { CoreRequest::PlayStone(request) => {
let mut app_state = self.state.write().unwrap(); // let mut app_state = self.state.write().unwrap();
app_state.place_stone(request); // app_state.place_stone(request);
let game = app_state.game.as_ref().unwrap(); // let game = app_state.game.as_ref().unwrap();
CoreResponse::PlayingFieldView(playing_field(game)) // CoreResponse::PlayingFieldView(playing_field(game))
unimplemented!()
} }
CoreRequest::StartGame => { CoreRequest::StartGame => {
unimplemented!() unimplemented!()

View File

@ -12,6 +12,6 @@ pub use board::*;
mod database; mod database;
mod types; mod types;
pub use types::{BoardError, Color, Rank, Size}; pub use types::{BoardError, Color, Rank, Size, DatabasePath, Config, ConfigOption};
pub mod ui; pub mod ui;

View File

@ -25,6 +25,12 @@ impl std::ops::Deref for DatabasePath {
} }
} }
impl From<String> for DatabasePath {
fn from(s: String) -> Self {
Self(PathBuf::from(s))
}
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, ConfigOption)] #[derive(Clone, Debug, PartialEq, Serialize, Deserialize, ConfigOption)]
pub struct Me(Player); pub struct Me(Player);

View File

@ -1,7 +1,7 @@
fn main() { fn main() {
glib_build_tools::compile_resources( glib_build_tools::compile_resources(
&["resources"], &["resources"],
"resources/gresources.xml", "gresources.xml",
"com.luminescent-dreams.kifu-gtk.gresource", "com.luminescent-dreams.kifu-gtk.gresource",
); );
} }

View File

@ -4,11 +4,11 @@ let
gsettingsDir = "${attrs.crateName}-${attrs.version}"; gsettingsDir = "${attrs.crateName}-${attrs.version}";
in { in {
nativeBuildInputs = gtkNativeInputs; nativeBuildInputs = gtkNativeInputs;
# postInstall = '' postInstall = ''
# install -Dt $out/share/applications resources/kifu.desktop install -Dt $out/share/applications resources/kifu.desktop
# install -Dt $out/gsettings-schemas/${gsettingsDir}/glib-2.0/schemas resources/com.luminescent-dreams.fitnesstrax.gschema.xml install -Dt $out/gsettings-schemas/${gsettingsDir}/glib-2.0/schemas resources/com.luminescent-dreams.kifu.gschema.xml
# glib-compile-schemas $out/gsettings-schemas/${gsettingsDir}/glib-2.0/schemas glib-compile-schemas $out/gsettings-schemas/${gsettingsDir}/glib-2.0/schemas
# ''; '';
# preFixup = '' # preFixup = ''
# gappsWrapperArgs+=( # gappsWrapperArgs+=(
# --prefix XDG_DATA_DIRS : $out/gsettings-schemas/${gsettingsDir} # --prefix XDG_DATA_DIRS : $out/gsettings-schemas/${gsettingsDir}

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<schemalist>
<schema id="com.luminescent-dreams.kifu-gtk.dev" path="/com/luminescent-dreams/kifu-gtk/dev/">
<key name="database-path" type="s">
<default>""</default>
<summary>Path to the directory of games</summary>
</key>
<key name="language" type="s">
<default>""</default>
<summary>Language override, use system settings if empty</summary>
</key>
</schema>
</schemalist>

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<schemalist>
<schema id="com.luminescent-dreams.kifu-gtk" path="/com/luminescent-dreams/kifu-gtk/">
<key name="database-path" type="s">
<default>""</default>
<summary>Path to the directory of games</summary>
</key>
<key name="language" type="s">
<default>""</default>
<summary>Language override, use system settings if empty</summary>
</key>
</schema>
</schemalist>

View File

@ -0,0 +1,5 @@
[Desktop Entry]
Version=0.2
Type=Application
Name=Kifu
Exec=kifu

View File

@ -1,5 +1,5 @@
use adw::prelude::*; use adw::prelude::*;
use kifu_core::{CoreApp, CoreRequest, CoreResponse}; use kifu_core::{CoreApp, CoreRequest, CoreResponse, DatabasePath, Config, ConfigOption};
use kifu_gtk::{ use kifu_gtk::{
perftrace, perftrace,
ui::{AppWindow, ConfigurationPage, Home, PlayingField}, ui::{AppWindow, ConfigurationPage, Home, PlayingField},
@ -7,6 +7,12 @@ use kifu_gtk::{
}; };
use std::sync::{Arc, RwLock}; use std::sync::{Arc, RwLock};
const APP_ID_DEV: &str = "com.luminescent-dreams.kifu-gtk.dev";
const APP_ID_PROD: &str = "com.luminescent-dreams.kifu-gtk";
const RESOURCE_BASE_PATH: &str = "/com/luminescent-dreams/kifu-gtk/";
fn handle_response(api: CoreApi, app_window: &AppWindow, message: CoreResponse) { fn handle_response(api: CoreApi, app_window: &AppWindow, message: CoreResponse) {
let playing_field = Arc::new(RwLock::new(None)); let playing_field = Arc::new(RwLock::new(None));
match message { match message {
@ -48,6 +54,17 @@ fn main() {
gio::resources_register_include!("com.luminescent-dreams.kifu-gtk.gresource") gio::resources_register_include!("com.luminescent-dreams.kifu-gtk.gresource")
.expect("Failed to register resources"); .expect("Failed to register resources");
let app_id = if std::env::var_os("ENV") == Some("dev".into()) {
APP_ID_DEV
} else {
APP_ID_PROD
};
let settings = gio::Settings::new(app_id);
let db_path: String = settings.string("database-path").into();
let mut config = Config::new();
config.set(ConfigOption::DatabasePath(db_path.into()));
let runtime = Arc::new( let runtime = Arc::new(
tokio::runtime::Builder::new_multi_thread() tokio::runtime::Builder::new_multi_thread()
.enable_all() .enable_all()
@ -55,6 +72,7 @@ fn main() {
.unwrap(), .unwrap(),
); );
/*
let config_path = std::env::var("CONFIG") let config_path = std::env::var("CONFIG")
.map(std::path::PathBuf::from) .map(std::path::PathBuf::from)
.or({ .or({
@ -66,8 +84,9 @@ fn main() {
}) })
}) })
.expect("no config path could be found"); .expect("no config path could be found");
*/
let core = CoreApp::new(config_path); let core = CoreApp::new(config);
let core_handle = runtime.spawn({ let core_handle = runtime.spawn({
let core = core.clone(); let core = core.clone();