Skip to content

Commit

Permalink
load window geometry and position
Browse files Browse the repository at this point in the history
  • Loading branch information
TicClick committed Oct 17, 2024
1 parent d035f51 commit 3b34592
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 22 deletions.
23 changes: 23 additions & 0 deletions crates/steel_core/src/settings/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use serde::{Deserialize, Serialize};
pub struct Application {
#[serde(default)]
pub autoupdate: AutoUpdate,
#[serde(default)]
pub window: WindowGeometry,
}

#[derive(Clone, Debug, Default, Serialize, Deserialize)]
Expand All @@ -12,3 +14,24 @@ pub struct AutoUpdate {
#[serde(default)]
pub url: String,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct WindowGeometry {
pub x: i32,
pub y: i32,
pub width: i32,
pub height: i32,
pub display: i32,
}

impl Default for WindowGeometry {
fn default() -> Self {
Self {
x: 600,
y: 400,
height: 600,
width: 800,
display: 0,
}
}
}
17 changes: 6 additions & 11 deletions src/gui/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use steel_core::chat::{Chat, ChatLike, ChatState, ConnectionStatus, Message, Mes
use steel_core::ipc::updater::UpdateState;
use steel_core::ipc::{client::CoreClient, server::AppMessageIn};

use eframe::egui::{self, Theme};
use steel_core::settings::ThemeMode;
use eframe::egui;
use tokio::sync::mpsc::UnboundedSender;

use crate::core::settings::Settings;
Expand Down Expand Up @@ -49,10 +48,10 @@ pub struct UIState {
}

impl UIState {
pub fn new(app_queue_handle: UnboundedSender<AppMessageIn>) -> Self {
pub fn new(app_queue_handle: UnboundedSender<AppMessageIn>, settings: Settings) -> Self {
Self {
connection: ConnectionStatus::default(),
settings: Settings::default(),
settings,
chats: Vec::default(),
name_to_chat: BTreeMap::default(),
server_messages: Vec::default(),
Expand All @@ -73,14 +72,10 @@ impl UIState {
}
}

pub fn set_settings(&mut self, ctx: &egui::Context, settings: Settings) {
self.settings = settings;
ctx.set_pixels_per_point(self.settings.ui.scaling);
ctx.set_theme(match self.settings.ui.theme {
ThemeMode::Dark => Theme::Dark,
ThemeMode::Light => Theme::Light,
});
pub fn update_settings(&mut self, settings: &Settings) {
self.settings = settings.clone();

// FIXME: Move this to a separate setter.
self.highlights
.set_highlights(&self.settings.notifications.highlights.words);
}
Expand Down
45 changes: 34 additions & 11 deletions src/gui/window.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use eframe::egui;
use eframe::egui::{self, Theme};
use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender};

use crate::gui;

use crate::gui::state::UIState;
use steel_core::{
ipc::{server::AppMessageIn, ui::UIMessageIn},
settings::Settings,
settings::{Settings, ThemeMode},
};

const UI_EVENT_INTAKE_PER_REFRESH: u32 = 100;
Expand Down Expand Up @@ -87,6 +87,34 @@ fn setup_custom_fonts(ctx: &egui::Context) {
ctx.set_fonts(fonts);
}

fn set_startup_ui_settings(ctx: &egui::Context, settings: &Settings) {
setup_custom_fonts(ctx);

ctx.style_mut(|style| {
style.url_in_tooltip = true;
});

ctx.send_viewport_cmd(egui::ViewportCommand::OuterPosition(egui::Pos2 {
x: settings.application.window.x as f32,
y: settings.application.window.y as f32,
}));

ctx.send_viewport_cmd(egui::ViewportCommand::InnerSize(egui::Vec2 {
x: settings.application.window.width as f32,
y: settings.application.window.height as f32,
}));

update_ui_settings(ctx, settings);
}

fn update_ui_settings(ctx: &egui::Context, settings: &Settings) {
ctx.set_pixels_per_point(settings.ui.scaling);
ctx.set_theme(match settings.ui.theme {
ThemeMode::Dark => Theme::Dark,
ThemeMode::Light => Theme::Light,
});
}

pub struct ApplicationWindow {
menu: gui::menu::Menu,
chat: gui::chat::ChatWindow,
Expand All @@ -108,14 +136,8 @@ impl ApplicationWindow {
app_queue_handle: UnboundedSender<AppMessageIn>,
initial_settings: Settings,
) -> Self {
setup_custom_fonts(&cc.egui_ctx);

let mut state = UIState::new(app_queue_handle);
state.set_settings(&cc.egui_ctx, initial_settings);

cc.egui_ctx.style_mut(|style| {
style.url_in_tooltip = true;
});
let state = UIState::new(app_queue_handle, initial_settings.clone());
set_startup_ui_settings(&cc.egui_ctx, &initial_settings);

Self {
menu: gui::menu::Menu::new(),
Expand Down Expand Up @@ -165,7 +187,8 @@ impl ApplicationWindow {
}

UIMessageIn::SettingsChanged(settings) => {
self.s.set_settings(ctx, settings);
update_ui_settings(ctx, &settings);
self.s.update_settings(&settings);
}

UIMessageIn::ConnectionStatusChanged(conn) => {
Expand Down

0 comments on commit 3b34592

Please sign in to comment.