Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for moderation settings #113

Merged
merged 1 commit into from
May 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ winresource = "0.1"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
steel_core = { version = "0.1.4", path = "./crates/steel_core" }
steel_core = { version = "*", path = "./crates/steel_core" }
glass = { version = "*", path = "./crates/glass", optional = true}

eframe = "0.25.0"
Expand Down
2 changes: 1 addition & 1 deletion crates/glass
Submodule glass updated from 4a3f19 to 71b6c9
2 changes: 1 addition & 1 deletion crates/steel_core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "steel_core"
version = "0.1.5"
version = "0.2.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
3 changes: 2 additions & 1 deletion crates/steel_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl VersionString for String {
}
}

#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum TextStyle {
Bold,
Italics,
Expand All @@ -47,4 +47,5 @@ pub enum TextStyle {
Monospace,

Highlight,
Coloured(ecolor::Color32),
}
64 changes: 12 additions & 52 deletions crates/steel_core/src/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ pub mod journal;
pub mod notifications;
pub mod ui;

use std::env;
use std::io::Write;

use serde::{Deserialize, Serialize};
Expand All @@ -21,25 +20,19 @@ pub use ui::{ChatColours, ThemeMode, UI};
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct Settings {
#[serde(skip)]
settings_path: Source,

pub application: Application,
pub chat: Chat,
pub notifications: Notifications,
pub ui: UI,
pub journal: Journal,
}

impl Settings {
pub fn from_file(source: &Source, fallback: bool) -> Self {
let path = source.expand();
match std::fs::read_to_string(&path) {
Ok(contents) => match serde_yaml::from_str::<Settings>(contents.as_str()) {
Ok(mut obj) => {
obj.settings_path = source.to_owned();
obj
}
pub trait Loadable: Sized + Default + Serialize + for<'de> Deserialize<'de> {
fn from_file(source: &str, fallback: bool) -> Self {
log::info!("Loading settings from {:?}", source);
match std::fs::read_to_string(source) {
Ok(contents) => match serde_yaml::from_str::<Self>(&contents) {
Ok(obj) => obj,
Err(e) => {
panic!("Error while loading the config: {}", e);
}
Expand All @@ -48,61 +41,28 @@ impl Settings {
if fallback {
return Self::default();
}
panic!("Error reading file at {}: {}", path, e);
panic!("Error reading file at {:?}: {}", source, e);
}
}
}

pub fn to_file(&self, path: &Source) {
let p = path.expand();
fn to_file(&self, path: &str) {
match serde_yaml::to_string(self) {
Ok(s) => match std::fs::File::create(&p) {
Ok(s) => match std::fs::File::create(path) {
Ok(mut f) => {
if f.write(s.as_bytes()).is_err() {
panic!("Failed to save settings")
}
}
Err(e) => {
panic!("Failed to create the file at {}: {}", p, e);
panic!("Failed to save settings to {:?}: {}", path, e);
}
},
Err(e) => {
panic!("Error saving config: {}", e);
panic!("Error saving settings: {}", e);
}
}
}

pub fn save(&self) {
self.to_file(&self.settings_path);
}

pub fn reload(&mut self) {
*self = Self::from_file(&self.settings_path, false);
}
}

const DEFAULT_FILE_NAME: &str = "settings.yaml";

#[derive(Clone, Debug, Default)]
pub enum Source {
#[default]
DefaultPath,
CustomPath(String),
}

impl Source {
pub fn expand(&self) -> String {
match self {
Source::DefaultPath => match env::current_dir() {
Ok(p) => p.join(DEFAULT_FILE_NAME).display().to_string(),
Err(e) => {
panic!(
"Failed to read current directory while looking for {}: {}",
DEFAULT_FILE_NAME, e
)
}
},
Source::CustomPath(p) => p.to_owned(),
}
}
}
impl Loadable for Settings {}
10 changes: 6 additions & 4 deletions src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::collections::BTreeSet;

use steel_core::ipc::updater::UpdateState;
use steel_core::settings::application::AutoUpdate;
use steel_core::settings::Loadable;
use tokio::sync::mpsc::{channel, Receiver, Sender};

use steel_core::chat::irc::IRCError;
Expand All @@ -12,6 +13,7 @@ use crate::core::updater::Updater;
use crate::core::{settings, updater};
use steel_core::ipc::{server::AppMessageIn, ui::UIMessageIn};

const DEFAULT_SETTINGS_PATH: &str = "settings.yaml";
const EVENT_QUEUE_SIZE: usize = 1000;

#[derive(Clone, Default)]
Expand Down Expand Up @@ -173,7 +175,7 @@ impl Application {
}

pub fn initialize(&mut self) {
self.load_settings(settings::Source::DefaultPath, true);
self.load_settings(true);
log::set_max_level(self.state.settings.journal.app_events.level);

self.start_updater();
Expand All @@ -182,8 +184,8 @@ impl Application {
}
}

pub fn load_settings(&mut self, source: settings::Source, fallback: bool) {
self.state.settings = settings::Settings::from_file(&source, fallback);
pub fn load_settings(&mut self, fallback: bool) {
self.state.settings = settings::Settings::from_file(DEFAULT_SETTINGS_PATH, fallback);

if self.state.settings.application.autoupdate.url.is_empty() {
self.state.settings.application.autoupdate.url = updater::default_update_url();
Expand All @@ -201,7 +203,7 @@ impl Application {

pub fn ui_handle_settings_updated(&mut self, settings: settings::Settings) {
self.state.settings = settings;
self.state.settings.save();
self.state.settings.to_file(DEFAULT_SETTINGS_PATH);
}

pub fn ui_request_usage_window(&mut self) {
Expand Down
33 changes: 16 additions & 17 deletions src/gui/chat.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use eframe::egui;
use egui_extras::{Column, TableBuilder};
use std::collections::{BTreeMap, BTreeSet};
use std::collections::BTreeMap;
use steel_core::chat::links::{Action, LinkType};

use steel_core::TextStyle;
Expand Down Expand Up @@ -246,11 +246,7 @@ impl ChatWindow {
} else {
match state.active_chat_tab_name.as_str() {
super::SERVER_TAB_NAME => {
let server_tab_styles = Some({
let mut st = BTreeSet::new();
st.insert(TextStyle::Monospace);
st
});
let server_tab_styles = Some(vec![TextStyle::Monospace]);
body.heterogeneous_rows(heights, |mut row| {
let row_index = row.index();
last_visible_row = row_index;
Expand Down Expand Up @@ -305,24 +301,27 @@ impl ChatWindow {
) {
// let msg = &ch.messages[message_index];
#[allow(unused_mut)] // glass
let mut username_styles = BTreeSet::<TextStyle>::new();
let mut message_styles = BTreeSet::<TextStyle>::new();
let mut username_styles = Vec::new();
let mut message_styles = Vec::new();

#[cfg(feature = "glass")]
{
if let Some(st) = state.glass.style_username(&ch.name, msg) {
username_styles.insert(st);
if let Some(st) = state
.glass
.style_username(&ch.name, msg, &state.settings.ui.theme)
{
username_styles.push(st);
}
if let Some(st) = state.glass.style_message(&ch.name, msg) {
message_styles.insert(st);
message_styles.push(st);
}
}

if msg.highlight {
message_styles.insert(TextStyle::Highlight);
message_styles.push(TextStyle::Highlight);
}
if matches!(msg.r#type, MessageType::Action) {
message_styles.insert(TextStyle::Italics);
message_styles.push(TextStyle::Italics);
}

let updated_height = ui
Expand Down Expand Up @@ -375,7 +374,7 @@ impl ChatWindow {
ui: &mut egui::Ui,
state: &UIState,
message_index: usize,
styles: &Option<BTreeSet<TextStyle>>,
styles: &Option<Vec<TextStyle>>,
) {
let msg = &state.server_messages[message_index];
let updated_height = ui
Expand Down Expand Up @@ -408,7 +407,7 @@ impl ChatWindow {
state: &UIState,
chat_name: &str,
msg: &Message,
styles: &Option<BTreeSet<TextStyle>>,
styles: &Option<Vec<TextStyle>>,
) {
let username_text = if msg.username == state.settings.chat.irc.username {
egui::RichText::new(&msg.username).color(state.settings.ui.colours().own.clone())
Expand Down Expand Up @@ -488,7 +487,7 @@ fn show_datetime(
ui: &mut egui::Ui,
state: &UIState,
msg: &Message,
styles: &Option<BTreeSet<TextStyle>>,
styles: &Option<Vec<TextStyle>>,
) -> egui::Response {
let timestamp = egui::RichText::new(msg.formatted_time()).with_styles(styles, &state.settings);
ui.label(timestamp).on_hover_ui_at_pointer(|ui| {
Expand Down Expand Up @@ -586,7 +585,7 @@ fn format_chat_message_text(
ui: &mut egui::Ui,
state: &UIState,
msg: &Message,
styles: &Option<BTreeSet<TextStyle>>,
styles: &Option<Vec<TextStyle>>,
) -> f32 {
let layout = egui::Layout::from_main_dir_and_cross_align(
egui::Direction::LeftToRight,
Expand Down
Loading
Loading