Skip to content

Commit a0c2ede

Browse files
committed
feat: use raylib::prelude::RaylibGuiState::gui_load_style_from_memory
1 parent e4aff02 commit a0c2ede

3 files changed

Lines changed: 28 additions & 49 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
### Changed
44

5+
- Drop `app::controller::{write_temp_style_file, load_style_from_memory}`
6+
- Use `raylib::prelude::RaylibGuiState::gui_load_style_from_memory`
57
- Bump `raylib` to `6.0.0-rc.2`
68
- Bump `pdf-writer` to `0.15.0`
79

src/app/controller.rs

Lines changed: 20 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,15 @@ use raylib::consts::GuiControl::DEFAULT;
2525
use raylib::consts::GuiControlProperty::TEXT_COLOR_NORMAL;
2626
use raylib::consts::GuiDefaultProperty::{BACKGROUND_COLOR, TEXT_SIZE, TEXT_SPACING};
2727
use raylib::consts::KeyboardKey::*;
28+
use raylib::error::LoadStyleFromMemoryError;
2829
use raylib::prelude::RaylibGuiState;
2930
use raylib::RaylibHandle;
30-
use std::fs::File;
31-
use std::io::Write;
32-
use std::path::PathBuf;
33-
use uuid::Uuid;
3431

3532
pub(crate) fn update_main(
3633
rl: &mut RaylibHandle,
3734
actions: &mut Vec<MainAction>,
3835
main_state: &mut MainState,
39-
) {
36+
) -> Result<(), LoadStyleFromMemoryError> {
4037
main_state.should_quit = rl.window_should_close();
4138

4239
main_state.frame_counter += 1;
@@ -70,7 +67,7 @@ pub(crate) fn update_main(
7067
main_state.current_font_height = main_state.default_font_height;
7168
rl.gui_set_style(DEFAULT, TEXT_SIZE, main_state.current_font_height);
7269
main_state.gui_theme_combobox_active = GuiTheme::Light as i32;
73-
GuiTheme::load_and_set(&GuiTheme::Light, rl, main_state);
70+
GuiTheme::load_and_set(&GuiTheme::Light, rl, main_state)?;
7471
let locale = get_locale();
7572
main_state.locale_combobox_active = locale as i32;
7673
main_state.locale = locale;
@@ -108,7 +105,7 @@ pub(crate) fn update_main(
108105
match <GuiTheme as TryFrom<i32>>::try_from(theme_idx) {
109106
Ok(t) => {
110107
main_state.gui_theme_combobox_active = theme_idx;
111-
t.load_and_set(rl, main_state);
108+
t.load_and_set(rl, main_state)?;
112109
}
113110
Err(_) => eprintln!("unknown number in current theme check"),
114111
}
@@ -134,76 +131,50 @@ pub(crate) fn update_main(
134131
if rl.is_key_pressed(KEY_F7) {
135132
main_state.showing_info_box = true;
136133
}
137-
}
138-
139-
fn write_temp_style_file(data: &[u8]) -> Result<(String, PathBuf), Box<dyn std::error::Error>> {
140-
// We employ a UUID to randomise the filename, as required
141-
// to avoid insecure temporary files vulnerabilities
142-
// See: https://doc.rust-lang.org/nightly/std/env/fn.temp_dir.html
143-
let mut temp_path = std::env::temp_dir();
144-
let id = Uuid::new_v4();
145-
temp_path.push(format!("{}.rgs", id));
146-
147-
let mut file = File::create(&temp_path)?;
148-
file.write_all(data)?;
149-
150-
let string = String::from(temp_path.to_string_lossy());
151-
Ok((string, temp_path))
152-
}
153-
154-
fn load_style_from_memory(rl: &mut RaylibHandle, data: &[u8]) {
155-
// Al momento, raylib-rs non espone una funzione gui_load_style_from_memory().
156-
// Qui simuliamo la disponibilità runtime di un file .rgs, partendo dai byte
157-
// di include_bytes!(). Non la migliore idea, ma sembra funzionare.
158-
159-
// Write the data to a temporary file
160-
let (temp_file_cstring, temp_file_path) =
161-
write_temp_style_file(data).expect("Failed to write temp style file");
162-
163-
// Load the style
164-
rl.gui_load_style(temp_file_cstring.as_str());
165-
166-
// Remove the temp file after loading the style
167-
std::fs::remove_file(temp_file_path).expect("Failed to delete temp style file");
134+
Ok(())
168135
}
169136

170137
impl GuiTheme {
171-
fn load_and_set(&self, rl: &mut RaylibHandle, main_state: &mut MainState) {
138+
fn load_and_set(
139+
&self,
140+
rl: &mut RaylibHandle,
141+
main_state: &mut MainState,
142+
) -> Result<(), LoadStyleFromMemoryError> {
172143
match self {
173144
GuiTheme::Dark => {
174-
load_style_from_memory(rl, DARK_THEME_DATA);
145+
rl.gui_load_style_from_memory(DARK_THEME_DATA)?;
175146
main_state.theme = GuiTheme::Dark;
176147
}
177148
GuiTheme::Bluish => {
178-
load_style_from_memory(rl, BLUISH_THEME_DATA);
149+
rl.gui_load_style_from_memory(BLUISH_THEME_DATA)?;
179150
main_state.theme = GuiTheme::Bluish;
180151
}
181152
GuiTheme::Candy => {
182-
load_style_from_memory(rl, CANDY_THEME_DATA);
153+
rl.gui_load_style_from_memory(CANDY_THEME_DATA)?;
183154
main_state.theme = GuiTheme::Candy;
184155
}
185156
GuiTheme::Cherry => {
186-
load_style_from_memory(rl, CHERRY_THEME_DATA);
157+
rl.gui_load_style_from_memory(CHERRY_THEME_DATA)?;
187158
main_state.theme = GuiTheme::Cherry;
188159
}
189160
GuiTheme::Cyber => {
190-
load_style_from_memory(rl, CYBER_THEME_DATA);
161+
rl.gui_load_style_from_memory(CYBER_THEME_DATA)?;
191162
main_state.theme = GuiTheme::Cyber;
192163
}
193164
GuiTheme::Jungle => {
194-
load_style_from_memory(rl, JUNGLE_THEME_DATA);
165+
rl.gui_load_style_from_memory(JUNGLE_THEME_DATA)?;
195166
main_state.theme = GuiTheme::Jungle;
196167
}
197168
GuiTheme::Lavanda => {
198-
load_style_from_memory(rl, LAVANDA_THEME_DATA);
169+
rl.gui_load_style_from_memory(LAVANDA_THEME_DATA)?;
199170
main_state.theme = GuiTheme::Lavanda;
200171
}
201172
GuiTheme::Terminal => {
202-
load_style_from_memory(rl, TERMINAL_THEME_DATA);
173+
rl.gui_load_style_from_memory(TERMINAL_THEME_DATA)?;
203174
main_state.theme = GuiTheme::Terminal;
204175
}
205176
GuiTheme::Ashes => {
206-
load_style_from_memory(rl, ASHES_THEME_DATA);
177+
rl.gui_load_style_from_memory(ASHES_THEME_DATA)?;
207178
main_state.theme = GuiTheme::Ashes;
208179
}
209180
GuiTheme::Light => {
@@ -230,5 +201,6 @@ impl GuiTheme {
230201
main_state.colors.default_txt_color = Color::get_color(txt_color_int);
231202
main_state.colors.default_bg_color = Color::get_color(bg_color_int);
232203
main_state.current_font = rl.gui_get_font();
204+
Ok(())
233205
}
234206
}

src/app/core.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,12 @@ impl App {
519519
let mut main_actions = Vec::<MainAction>::new();
520520
while !self.main_state.should_quit {
521521
// Base update step
522-
update_main(&mut self.rl, &mut main_actions, &mut self.main_state);
522+
if let Err(load_style_error) =
523+
update_main(&mut self.rl, &mut main_actions, &mut self.main_state)
524+
{
525+
eprintln!("Error while loading theme: {load_style_error}");
526+
// TODO: print error in GUI console too and put user there?
527+
}
523528

524529
self.controllers.update(
525530
&mut self.rl,

0 commit comments

Comments
 (0)