Skip to content

Commit f8b8ca7

Browse files
committed
unified spacing config
1 parent a1fc715 commit f8b8ca7

File tree

9 files changed

+277
-20
lines changed

9 files changed

+277
-20
lines changed

book/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
- [Notifications](configuration/notifications/README.md)
7777
- [Pane](configuration/pane/README.md)
7878
- [Gap](configuration/pane/gap.md)
79+
- [Spacing](configuration/spacing/README.md)
7980
- [Platform Specific](configuration/platform-specific/README.md)
8081
- [Linux](configuration/platform-specific/linux.md)
8182
- [macOS](configuration/platform-specific/macos.md)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Spacing
2+
3+
Spacing settings for Halloy.
4+
5+
This section provides aliases for spacing-related options that also exist under other sections (e.g. `[buffer]`, `[pane]`, `[sidebar]`, `[context_menu]`). Values set here take precedence when both are present.
6+
7+
## Configuration
8+
9+
```toml
10+
[spacing.buffer]
11+
# Alias of: [buffer] line_spacing
12+
line_spacing = 4
13+
14+
[spacing.pane.gap]
15+
# Alias of: [pane.gap] inner / outer
16+
inner = 6
17+
outer = 10
18+
19+
[spacing.sidebar.padding]
20+
# Alias of: [sidebar.padding] buffer
21+
buffer = [5, 5]
22+
23+
[spacing.sidebar.spacing]
24+
# Alias of: [sidebar.spacing] server
25+
server = 12
26+
27+
[spacing.context_menu.padding]
28+
# Alias of: [context_menu.padding] entry
29+
entry = [2, 5]
30+
```

data/src/config.rs

Lines changed: 92 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub use self::preview::Preview;
2525
pub use self::proxy::Proxy;
2626
pub use self::server::Server;
2727
pub use self::sidebar::Sidebar;
28+
pub use self::spacing::Spacing;
2829
use crate::appearance::theme::Styles;
2930
use crate::appearance::{self, Appearance};
3031
use crate::audio::{self};
@@ -48,6 +49,7 @@ pub mod preview;
4849
pub mod proxy;
4950
pub mod server;
5051
pub mod sidebar;
52+
pub mod spacing;
5153

5254
const CONFIG_TEMPLATE: &str = include_str!("../../config.toml");
5355
const DEFAULT_THEME_NAME: &str = "ferra";
@@ -60,6 +62,7 @@ pub struct Config {
6062
pub proxy: Option<Proxy>,
6163
pub font: Font,
6264
pub scale_factor: ScaleFactor,
65+
pub spacing: Spacing,
6366
pub buffer: Buffer,
6467
pub pane: Pane,
6568
pub sidebar: Sidebar,
@@ -312,6 +315,7 @@ impl Config {
312315
pub proxy: Option<Proxy>,
313316
pub font: Font,
314317
pub scale_factor: ScaleFactor,
318+
pub spacing: Spacing,
315319
pub buffer: Buffer,
316320
pub pane: Pane,
317321
pub sidebar: Sidebar,
@@ -336,6 +340,7 @@ impl Config {
336340
proxy: None,
337341
font: Font::default(),
338342
scale_factor: ScaleFactor::default(),
343+
spacing: Spacing::default(),
339344
buffer: Buffer::default(),
340345
pane: Pane::default(),
341346
sidebar: Sidebar::default(),
@@ -353,6 +358,62 @@ impl Config {
353358
}
354359
}
355360

361+
fn migrate_spacing_from_legacy(
362+
config: &mut Configuration,
363+
raw_toml: &toml::Value,
364+
) {
365+
if !toml_path_exists(
366+
raw_toml,
367+
&["spacing", "buffer", "line_spacing"],
368+
) && toml_path_exists(raw_toml, &["buffer", "line_spacing"])
369+
{
370+
config.spacing.buffer.line_spacing = config.buffer.line_spacing;
371+
}
372+
373+
if !toml_path_exists(raw_toml, &["spacing", "pane", "gap", "inner"])
374+
&& toml_path_exists(raw_toml, &["pane", "gap", "inner"])
375+
{
376+
config.spacing.pane.gap.inner = config.pane.gap.inner;
377+
}
378+
if !toml_path_exists(raw_toml, &["spacing", "pane", "gap", "outer"])
379+
&& toml_path_exists(raw_toml, &["pane", "gap", "outer"])
380+
{
381+
config.spacing.pane.gap.outer = config.pane.gap.outer;
382+
}
383+
384+
if !toml_path_exists(
385+
raw_toml,
386+
&["spacing", "sidebar", "padding", "buffer"],
387+
) && toml_path_exists(
388+
raw_toml,
389+
&["sidebar", "padding", "buffer"],
390+
) {
391+
config.spacing.sidebar.padding.buffer =
392+
config.sidebar.padding.buffer;
393+
}
394+
if !toml_path_exists(
395+
raw_toml,
396+
&["spacing", "sidebar", "spacing", "server"],
397+
) && toml_path_exists(
398+
raw_toml,
399+
&["sidebar", "spacing", "server"],
400+
) {
401+
config.spacing.sidebar.spacing.server =
402+
config.sidebar.spacing.server;
403+
}
404+
405+
if !toml_path_exists(
406+
raw_toml,
407+
&["spacing", "context_menu", "padding", "entry"],
408+
) && toml_path_exists(
409+
raw_toml,
410+
&["context_menu", "padding", "entry"],
411+
) {
412+
config.spacing.context_menu.padding.entry =
413+
config.context_menu.padding.entry;
414+
}
415+
}
416+
356417
let path = Self::path();
357418
if !path.try_exists()? {
358419
return Err(Error::ConfigMissing);
@@ -363,13 +424,25 @@ impl Config {
363424

364425
let config = toml::Deserializer::new(content.as_ref());
365426

427+
let mut configuration: Configuration =
428+
serde_ignored::deserialize(config, |ignored| {
429+
log::warn!("[config.toml] Ignoring unknown setting: {ignored}");
430+
})
431+
.map_err(|e| Error::Parse(e.to_string()))?;
432+
433+
let raw_toml: toml::Value = toml::from_str(content.as_ref())
434+
.map_err(|e| Error::Parse(e.to_string()))?;
435+
436+
migrate_spacing_from_legacy(&mut configuration, &raw_toml);
437+
366438
let Configuration {
367439
theme,
368440
servers,
369441
context_menu,
370442
font,
371443
proxy,
372444
scale_factor,
445+
spacing,
373446
buffer,
374447
sidebar,
375448
keyboard,
@@ -383,10 +456,7 @@ impl Config {
383456
ctcp,
384457
logs,
385458
platform_specific,
386-
} = serde_ignored::deserialize(config, |ignored| {
387-
log::warn!("[config.toml] Ignoring unknown setting: {ignored}");
388-
})
389-
.map_err(|e| Error::Parse(e.to_string()))?;
459+
} = configuration;
390460

391461
let servers = ServerMap::new(servers).await?;
392462

@@ -401,6 +471,7 @@ impl Config {
401471
font,
402472
proxy,
403473
scale_factor,
474+
spacing,
404475
buffer,
405476
sidebar,
406477
keyboard,
@@ -530,6 +601,23 @@ impl Config {
530601
}
531602
}
532603

604+
fn toml_path_exists(root: &toml::Value, path: &[&str]) -> bool {
605+
let mut current = root;
606+
607+
for key in path {
608+
let Some(table) = current.as_table() else {
609+
return false;
610+
};
611+
let Some(value) = table.get(*key) else {
612+
return false;
613+
};
614+
615+
current = value;
616+
}
617+
618+
true
619+
}
620+
533621
pub fn random_nickname() -> String {
534622
let mut rng = ChaCha8Rng::from_rng(&mut rand::rng());
535623
random_nickname_with_seed(&mut rng)

data/src/config/spacing.rs

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
use serde::Deserialize;
2+
3+
#[derive(Debug, Clone, Copy, Deserialize)]
4+
#[serde(default)]
5+
pub struct Spacing {
6+
pub buffer: Buffer,
7+
pub pane: Pane,
8+
pub sidebar: Sidebar,
9+
pub context_menu: ContextMenu,
10+
}
11+
12+
impl Default for Spacing {
13+
fn default() -> Self {
14+
Self {
15+
buffer: Buffer::default(),
16+
pane: Pane::default(),
17+
sidebar: Sidebar::default(),
18+
context_menu: ContextMenu::default(),
19+
}
20+
}
21+
}
22+
23+
#[derive(Debug, Clone, Copy, Deserialize)]
24+
#[serde(default)]
25+
pub struct Buffer {
26+
pub line_spacing: u32,
27+
}
28+
29+
impl Default for Buffer {
30+
fn default() -> Self {
31+
Self { line_spacing: 0 }
32+
}
33+
}
34+
35+
#[derive(Debug, Clone, Copy, Deserialize)]
36+
#[serde(default)]
37+
pub struct Pane {
38+
pub gap: Gap,
39+
}
40+
41+
impl Default for Pane {
42+
fn default() -> Self {
43+
Self {
44+
gap: Gap::default(),
45+
}
46+
}
47+
}
48+
49+
#[derive(Debug, Clone, Copy, Deserialize)]
50+
#[serde(default)]
51+
pub struct Gap {
52+
pub inner: u32,
53+
pub outer: u16,
54+
}
55+
56+
impl Default for Gap {
57+
fn default() -> Self {
58+
Self { inner: 4, outer: 8 }
59+
}
60+
}
61+
62+
#[derive(Debug, Clone, Copy, Deserialize)]
63+
#[serde(default)]
64+
pub struct Sidebar {
65+
pub padding: SidebarPadding,
66+
pub spacing: SidebarSpacing,
67+
}
68+
69+
impl Default for Sidebar {
70+
fn default() -> Self {
71+
Self {
72+
padding: SidebarPadding::default(),
73+
spacing: SidebarSpacing::default(),
74+
}
75+
}
76+
}
77+
78+
#[derive(Debug, Clone, Copy, Deserialize)]
79+
#[serde(default)]
80+
pub struct SidebarPadding {
81+
pub buffer: [u16; 2],
82+
}
83+
84+
impl Default for SidebarPadding {
85+
fn default() -> Self {
86+
Self { buffer: [5, 5] }
87+
}
88+
}
89+
90+
#[derive(Debug, Clone, Copy, Deserialize)]
91+
#[serde(default)]
92+
pub struct SidebarSpacing {
93+
pub server: u32,
94+
}
95+
96+
impl Default for SidebarSpacing {
97+
fn default() -> Self {
98+
Self { server: 12 }
99+
}
100+
}
101+
102+
#[derive(Debug, Clone, Copy, Deserialize)]
103+
#[serde(default)]
104+
pub struct ContextMenu {
105+
pub padding: ContextMenuPadding,
106+
}
107+
108+
impl Default for ContextMenu {
109+
fn default() -> Self {
110+
Self {
111+
padding: ContextMenuPadding::default(),
112+
}
113+
}
114+
}
115+
116+
#[derive(Debug, Clone, Copy, Deserialize)]
117+
#[serde(default)]
118+
pub struct ContextMenuPadding {
119+
pub entry: [u16; 2],
120+
}
121+
122+
impl Default for ContextMenuPadding {
123+
fn default() -> Self {
124+
Self { entry: [5, 5] }
125+
}
126+
}

src/buffer/context_menu.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -580,14 +580,14 @@ fn menu_button(
580580
.style(theme::text::primary)
581581
.font_maybe(theme::font_style::primary(theme).map(font::get)),
582582
)
583-
.padding(config.context_menu.padding.entry)
583+
.padding(config.spacing.context_menu.padding.entry)
584584
.width(length)
585585
.on_press_maybe(message)
586586
.into()
587587
}
588588

589589
fn right_justified_padding(config: &Config) -> Padding {
590-
let padding = config.context_menu.padding.entry;
590+
let padding = config.spacing.context_menu.padding.entry;
591591
Padding::from(padding)
592592
.right(padding[1] as f32 + double_pass::horizontal_expansion())
593593
}

src/buffer/input_view.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ pub fn view<'a>(
426426
.align_y(iced::Alignment::Center),
427427
)
428428
.width(length)
429-
.padding(config.context_menu.padding.entry)
429+
.padding(config.spacing.context_menu.padding.entry)
430430
.on_press_maybe(message)
431431
.into()
432432
};

src/buffer/scroll_view.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -524,12 +524,12 @@ pub fn view<'a>(
524524
let content = on_resize(
525525
column![
526526
top_row,
527-
column(old).spacing(config.buffer.line_spacing),
527+
column(old).spacing(config.spacing.buffer.line_spacing),
528528
keyed(keyed::Key::Divider, divider),
529-
column(new).spacing(config.buffer.line_spacing),
530-
space::vertical().height(config.buffer.line_spacing),
529+
column(new).spacing(config.spacing.buffer.line_spacing),
530+
space::vertical().height(config.spacing.buffer.line_spacing),
531531
]
532-
.spacing(config.buffer.line_spacing),
532+
.spacing(config.spacing.buffer.line_spacing),
533533
Message::ContentResized,
534534
);
535535

0 commit comments

Comments
 (0)