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
5 changed files with 48 additions and 49 deletions
Showing only changes of commit 915028c32a - Show all commits

View File

@ -9,6 +9,11 @@ use std::{
path::PathBuf,
sync::{Arc, RwLock},
};
pub trait Observable<T> {
fn subscribe(&self) -> Receiver<T>;
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum CoreRequest {
ChangeSetting(ChangeSettingRequest),
@ -92,15 +97,6 @@ impl Core {
}
}
pub fn subscribe(&self) -> Receiver<CoreNotification> {
let mut subscribers = self.subscribers.write().unwrap();
let (sender, receiver) = async_std::channel::unbounded();
subscribers.push(sender);
receiver
}
/*
pub async fn dispatch(&self, request: CoreRequest) -> CoreResponse {
match request {
@ -168,3 +164,14 @@ impl Core {
// pub async fn run(&self) {}
}
impl Observable<CoreNotification> for Core {
fn subscribe(&self) -> Receiver<CoreNotification> {
let mut subscribers = self.subscribers.write().unwrap();
let (sender, receiver) = async_std::channel::unbounded();
subscribers.push(sender);
receiver
}
}

View File

@ -3,7 +3,7 @@ extern crate config_derive;
mod api;
pub use api::{
ChangeSettingRequest, Core, CoreNotification, CoreRequest, CoreResponse, CreateGameRequest,
HotseatPlayerRequest, PlayerInfoRequest,
HotseatPlayerRequest, Observable, PlayerInfoRequest,
};
mod board;

View File

@ -3,14 +3,12 @@ pub mod ui;
mod view_models;
mod views;
use kifu_core::{Core, CoreRequest, CoreResponse};
use std::sync::Arc;
use tokio::{spawn, runtime::Runtime};
use tokio::{runtime::Runtime, spawn};
#[derive(Clone)]
pub struct CoreApi {
pub notification_tx: async_channel::Sender<CoreResponse>,
pub rt: Arc<Runtime>,
pub core: Core,
}

View File

@ -1,5 +1,5 @@
use adw::prelude::*;
use kifu_core::{Core, CoreRequest, CoreResponse, DatabasePath, Config, ConfigOption};
use kifu_core::{Config, ConfigOption, Core, CoreRequest, CoreResponse, DatabasePath};
use kifu_gtk::{
perftrace,
ui::{AppWindow, ConfigurationPage, Home, PlayingField},
@ -104,16 +104,9 @@ fn main() {
app.connect_activate({
let runtime = runtime.clone();
move |app| {
let (notification_tx, notification_rx) = async_channel::unbounded::<CoreResponse>();
/*
let (gtk_tx, gtk_rx) =
gtk::glib::MainContext::channel::<CoreResponse>(gtk::glib::Priority::DEFAULT);
*/
let app_window = AppWindow::new(app);
let api = CoreApi {
notification_tx,
rt: runtime.clone(),
core: core.clone(),
};

View File

@ -14,13 +14,12 @@ General Public License for more details.
You should have received a copy of the GNU General Public License along with Kifu. If not, see <https://www.gnu.org/licenses/>.
*/
use async_channel::Receiver;
use async_std::task::{spawn, yield_now, JoinHandle};
use kifu_core::{Color, Core, CoreNotification, Goban, Player};
use std::{
sync::{Arc, RwLock},
time::Duration,
use async_std::{
channel::Receiver,
task::{spawn, yield_now, JoinHandle},
};
use kifu_core::{Color, Core, CoreNotification, Goban, Observable, Player};
use std::{cell::RefCell, rc::Rc, time::Duration};
pub struct GameState {
goban: Goban,
@ -41,9 +40,8 @@ struct GameViewModelPrivate {
/// The Game View Model manages the current state of the game. It shows the two player cards, the board, the current capture count, the current player, and it maintains the UI for the clock (bearing in mind that the real clock is managed in the core). This view model should only be created once the details of the game, whether a game in progress or a new game (this view model won't know the difference) is known.
pub struct GameViewModel {
core: Core,
notification_handler: JoinHandle<()>,
widget: gtk::Box,
data: Arc<RwLock<GameViewModelPrivate>>,
data: Rc<RefCell<GameViewModelPrivate>>,
}
impl GameViewModelPrivate {
@ -52,20 +50,32 @@ impl GameViewModelPrivate {
impl GameViewModel {
pub fn new(white: Player, black: Player, game: GameState, core: Core) -> Self {
let data = Arc::new(RwLock::new(GameViewModelPrivate {
let data = Rc::new(RefCell::new(GameViewModelPrivate {
white,
black,
state: game,
}));
let handler = spawn({
let core = core.clone();
let data = data.clone();
async move {
let notifications = core.subscribe();
let s = Self {
core,
widget: gtk::Box::new(gtk::Orientation::Horizontal, 0),
data,
};
let notifications = s.core.subscribe();
let data = s.data.clone();
glib::spawn_future_local(Self::listen(notifications, data));
s
}
async fn listen(
notifications: Receiver<CoreNotification>,
data: Rc<RefCell<GameViewModelPrivate>>,
) {
loop {
match notifications.recv().await {
Ok(msg) => data.write().unwrap().handle(msg),
Ok(msg) => data.borrow_mut().handle(msg),
Err(err) => {
unimplemented!("Should display an error message in the UI: {}", err)
}
@ -73,13 +83,4 @@ impl GameViewModel {
yield_now().await;
}
}
});
Self {
core,
notification_handler: handler,
widget: gtk::Box::new(gtk::Orientation::Horizontal, 0),
data,
}
}
}