From 669bcde3f49e681c4f486450fb52ba67c700ab30 Mon Sep 17 00:00:00 2001 From: Mike Dilger Date: Thu, 29 Dec 2022 15:20:52 +1300 Subject: [PATCH] Max FPS setting, and repaint at least once per second --- src/settings.rs | 7 ++++++- src/ui/mod.rs | 10 ++++++++++ src/ui/settings.rs | 11 ++++++++++- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/settings.rs b/src/settings.rs index c8451d88..7d71fad1 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -12,6 +12,7 @@ pub const DEFAULT_VIEW_POSTS_REFERRING_TO: bool = false; pub const DEFAULT_VIEW_THREADED: bool = true; pub const DEFAULT_NUM_RELAYS_PER_PERSON: u8 = 2; pub const DEFAULT_MAX_RELAYS: u8 = 15; +pub const DEFAULT_MAX_FPS: u32 = 60; #[derive(Clone, Debug, Deserialize, Serialize)] pub struct Settings { @@ -25,6 +26,7 @@ pub struct Settings { pub max_relays: u8, pub public_key: Option, pub encrypted_private_key: Option, + pub max_fps: u32, } impl Default for Settings { @@ -40,6 +42,7 @@ impl Default for Settings { max_relays: DEFAULT_MAX_RELAYS, public_key: None, encrypted_private_key: None, + max_fps: DEFAULT_MAX_FPS, } } } @@ -89,6 +92,7 @@ impl Settings { } } } + "max_fps" => settings.max_fps = row.1.parse::().unwrap_or(DEFAULT_MAX_FPS), _ => {} } } @@ -105,7 +109,7 @@ impl Settings { ('feed_chunk', ?),('overlap', ?),('autofollow', ?),\ ('view_posts_referred_to', ?),('view_posts_referring_to', ?),\ ('view_threaded', ?),('num_relays_per_person', ?), \ - ('max_relays', ?)", + ('max_relays', ?),('max_fps', ?)", )?; stmt.execute(( self.feed_chunk, @@ -124,6 +128,7 @@ impl Settings { if self.view_threaded { "1" } else { "0" }, self.num_relays_per_person, self.max_relays, + self.max_fps, ))?; // Save private key identity diff --git a/src/ui/mod.rs b/src/ui/mod.rs index a718a56b..0e863fcb 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -15,6 +15,7 @@ use crate::settings::Settings; use eframe::{egui, IconData, Theme}; use egui::{ColorImage, Context, ImageData, TextureHandle, TextureOptions}; use nostr_types::{PublicKey, PublicKeyHex}; +use std::time::{Duration, Instant}; use zeroize::Zeroize; pub fn run() -> Result<(), Error> { @@ -59,6 +60,7 @@ enum Page { } struct GossipUi { + next_frame: Instant, page: Page, about: About, icon: TextureHandle, @@ -123,6 +125,7 @@ impl GossipUi { let settings = GLOBALS.settings.blocking_read().clone(); GossipUi { + next_frame: Instant::now(), page: Page::Feed, about: crate::about::about(), icon: icon_texture_handle, @@ -142,6 +145,13 @@ impl GossipUi { impl eframe::App for GossipUi { fn update(&mut self, ctx: &Context, frame: &mut eframe::Frame) { + // Wait until the next frame + std::thread::sleep(self.next_frame - Instant::now()); + self.next_frame += Duration::from_secs_f32(1.0 / self.settings.max_fps as f32); + + // Redraw at least once per second + ctx.request_repaint_after(Duration::from_secs(1)); + if GLOBALS .shutting_down .load(std::sync::atomic::Ordering::Relaxed) diff --git a/src/ui/settings.rs b/src/ui/settings.rs index 3aedbad7..503b3aac 100644 --- a/src/ui/settings.rs +++ b/src/ui/settings.rs @@ -77,7 +77,7 @@ pub(super) fn update( .on_hover_text("Definately not recommended. In fact we may remove this soon."); ui.add_space(24.0); - ui.heading("Style"); + ui.heading("User Interface"); ui.horizontal(|ui| { ui.label("Switch to"); @@ -102,6 +102,15 @@ pub(super) fn update( } }); + ui.add_space(24.0); + ui.horizontal(|ui| { + ui.label("Maximum FPS: ") + .on_hover_text( + "The UI redraws every frame. By limiting the maximum FPS you can reduce load on your CPU. Takes effect immediately.", + ); + ui.add(Slider::new(&mut app.settings.max_fps, 10..=60).text("Frames per second")); + }); + ui.add_space(32.0); ui.with_layout(Layout::top_down(Align::Center), |ui| { if ui.button("SAVE CHANGES").clicked() {