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

feat: use height of text as button height in context menu #254

Closed
wants to merge 2 commits into from
Closed
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Unreleased

Fixed:

- Clipped context buttons on Linux when using a non-default font size

# 2024.3 (2024-03-05)

Added:
Expand Down
40 changes: 35 additions & 5 deletions data/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fs::{self, File};
use std::io::BufReader;
use std::path::PathBuf;

use serde::Deserialize;
use serde::{Deserialize, Deserializer};
use thiserror::Error;

pub use self::buffer::Buffer;
Expand Down Expand Up @@ -58,10 +58,40 @@ impl From<ScaleFactor> for f64 {
}
}

#[derive(Debug, Clone, Default, Deserialize)]
#[derive(Debug, Clone, Deserialize)]
pub struct Font {
pub family: Option<String>,
pub size: Option<u8>,
pub family: String,
pub size: f32,
}

impl Default for Font {
fn default() -> Self {
Self {
family: String::from("Iosevka Term"),
size: 13.0
}
}
}

impl Font {
fn deserialize<'de, D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
#[derive(Deserialize)]
struct MaybeFont {
family: Option<String>,
size: Option<f32>,
}

let MaybeFont { family, size } = MaybeFont::deserialize(deserializer)?;
let Font { family: default_family, size: default_size } = Font::default();

Ok(Font {
family: family.unwrap_or(default_family),
size: size.unwrap_or(default_size),
})
}
Comment on lines +63 to +94
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#[serde(default_with = "default_font_size")]

fn default_font_size() -> f32 {
  13.0
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then you can keep default below on Font

}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -112,7 +142,7 @@ impl Config {
#[serde(default)]
pub theme: String,
pub servers: ServerMap,
#[serde(default)]
#[serde(deserialize_with = "Font::deserialize")]
pub font: Font,
#[serde(default)]
pub scale_factor: ScaleFactor,
Expand Down
3 changes: 2 additions & 1 deletion src/buffer/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pub fn view<'a>(
),
),
user.clone(),
&config.font
)
.map(scroll_view::Message::UserContext);
let row_style = match our_nick {
Expand Down Expand Up @@ -248,7 +249,7 @@ mod nick_list {
user.is_away(),
));

user_context::view(content, user.clone())
user_context::view(content, user.clone(), &config.font)
}))
.padding(4)
.spacing(1);
Expand Down
1 change: 1 addition & 0 deletions src/buffer/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub fn view<'a>(
),
),
user.clone(),
&config.font,
)
.map(scroll_view::Message::UserContext);

Expand Down
9 changes: 7 additions & 2 deletions src/buffer/user_context.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use data::User;
use iced::widget::text::LineHeight;
use iced::widget::{button, text};

use crate::theme;
Expand Down Expand Up @@ -38,7 +39,8 @@ pub fn update(message: Message) -> Event {
}
}

pub fn view<'a>(content: impl Into<Element<'a, Message>>, user: User) -> Element<'a, Message> {
pub fn view<'a>(content: impl Into<Element<'a, Message>>, user: User, font: &data::config::Font) -> Element<'a, Message> {
let font = font.clone();
let entries = Entry::list();

let content = button(content)
Expand All @@ -52,9 +54,12 @@ pub fn view<'a>(content: impl Into<Element<'a, Message>>, user: User) -> Element
Entry::Query => ("Message", Message::Query(user.clone())),
};

// Height based on font size, with some margin added.
let height = LineHeight::default().to_absolute((font.size + 8.0).into());

button(text(content).style(theme::Text::Primary))
.width(length)
.height(length)
.height(height)
.style(theme::Button::Context)
.on_press(message)
.into()
Expand Down
8 changes: 3 additions & 5 deletions src/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,10 @@ impl From<Font> for iced::Font {
}

pub fn set(config: Option<&Config>) {
let family = config
.and_then(|config| config.font.family.clone())
.unwrap_or_else(|| String::from("Iosevka Term"));
let font = config.map(|config| config.font.clone()).unwrap_or_default();

MONO.set(family.clone());
MONO_BOLD.set(family);
MONO.set(font.family.clone());
MONO_BOLD.set(font.family);
}

pub fn load() -> Vec<Cow<'static, [u8]>> {
Expand Down
10 changes: 4 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,14 @@ pub fn main() -> iced::Result {
fn settings(
config_load: Result<Config, config::Error>,
) -> iced::Settings<Result<Config, config::Error>> {
let default_text_size = config_load

let font = config_load
.as_ref()
.ok()
.and_then(|config| config.font.size)
.map(f32::from)
.unwrap_or(theme::TEXT_SIZE);
.ok().map(|config| config.font.clone()).unwrap_or_default();

iced::Settings {
default_font: font::MONO.clone().into(),
default_text_size: default_text_size.into(),
default_text_size: font.size.into(),
window: window::Settings {
exit_on_close_request: false,
..window::settings()
Expand Down
1 change: 1 addition & 0 deletions src/screen/dashboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,7 @@ impl Dashboard {
&self.panes,
self.focus,
config.dashboard.sidebar,
&config.font,
)
.map(|e| e.map(Message::SideMenu));

Expand Down
2 changes: 1 addition & 1 deletion src/screen/dashboard/command_bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl CommandBar {
config: &'a Config,
) -> Element<'a, Message> {
// 1px larger than default
let font_size = config.font.size.map(f32::from).unwrap_or(theme::TEXT_SIZE) + 1.0;
let font_size = config.font.size + 1.0;

let combo_box = combo_box(&self.state, "Type a command...", None, Message::Command)
.on_close(Message::Unfocused)
Expand Down
13 changes: 12 additions & 1 deletion src/screen/dashboard/side_menu.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use data::dashboard::DefaultAction;
use data::{history, Buffer};
use iced::widget::text::LineHeight;
use iced::widget::{
button, column, container, horizontal_space, pane_grid, row, scrollable, text, vertical_space,
};
Expand Down Expand Up @@ -58,6 +59,7 @@ impl SideMenu {
panes: &pane_grid::State<Pane>,
focus: Option<pane_grid::Pane>,
config: data::config::dashboard::Sidebar,
font: &data::config::Font,
) -> Option<Element<'a, Message>> {
if self.hidden {
return None;
Expand All @@ -75,6 +77,7 @@ impl SideMenu {
false,
false,
config.default_action,
font,
));
}
data::client::State::Ready(connection) => {
Expand All @@ -85,6 +88,7 @@ impl SideMenu {
true,
false,
config.default_action,
font,
));

for channel in connection.channels() {
Expand All @@ -95,6 +99,7 @@ impl SideMenu {
true,
history.has_unread(server, &history::Kind::Channel(channel.clone())),
config.default_action,
font,
));
}

Expand All @@ -107,6 +112,7 @@ impl SideMenu {
true,
history.has_unread(server, &history::Kind::Query(user.clone())),
config.default_action,
font,
));
}

Expand Down Expand Up @@ -171,7 +177,9 @@ fn buffer_button<'a>(
connected: bool,
has_unread: bool,
default_action: DefaultAction,
font: &data::config::Font,
) -> Element<'a, Message> {
let font = font.clone();
let open = panes
.iter()
.find_map(|(pane, state)| (state.buffer.data().as_ref() == Some(&buffer)).then_some(*pane));
Expand Down Expand Up @@ -238,9 +246,12 @@ fn buffer_button<'a>(
),
};

// Height based on font size, with some margin added.
let height = LineHeight::default().to_absolute((font.size + 8.0).into());

button(text(content).style(theme::Text::Primary))
.width(length)
.height(length)
.height(height)
Comment on lines +249 to +254
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems super hacky. Let me play around. We should just be able to use Shrink height and have it work when font size increases....

.style(theme::Button::Context)
.on_press(message)
.into()
Expand Down
1 change: 0 additions & 1 deletion src/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use crate::widget::selectable_text;

// TODO: If we use non-standard font sizes, we should consider
// Config.font.size since it's user configurable
pub const TEXT_SIZE: f32 = 13.0;
pub const ICON_SIZE: f32 = 12.0;

#[derive(Debug, Clone)]
Expand Down
Loading