Skip to content

Commit

Permalink
Implement size constraints for content in zen-mode
Browse files Browse the repository at this point in the history
This commit will implement and document two options for setting constraints on the horizontal
and vertical length of the content in zen-mode.

Signed-off-by: Builditluc <[email protected]>
  • Loading branch information
Builditluc committed Nov 13, 2024
1 parent 63da1c8 commit d713a9d
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 4 deletions.
40 changes: 40 additions & 0 deletions docs/docs/configuration/page.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,46 @@ page.zen_mode.include = "TOC|SCROLLBAR"

![Zen](../assets/images/page-zen.png)

### Changing the size of the content in zen mode

[:octicons-tag-24: 0.9.0][release-0.9.0] · :octicons-milestone-16: Default `80` | `90`

You can change the size of the content in zen mode by setting the horizontal and vertical constraint
of the content. For example, this is the default configuration for the zen mode:

```toml
page.zen_mode.horizontal.percentage = 80
page.zen_mode.vertical.percentage = 90
```

Both of these settings share the same configuration schema. A constraint can have a fixed size or be a
relative constraint.

* **Min**: `page.zen_mode.{horizontal|vertical}.min = u16`
Applies a minimum size constraint to the element. The element size is set to at least the
specified amount.
* **Max**: `page.zen_mode.{horizontal|vertical}.max = u16`
Applies a maximum size constraint to the element. The element size is set to at most the
specified amount.
* **Length**: `page.zen_mode.{horizontal|vertical}.length = u16`
Applies a length constraint to the element. The element size is set to the specified amount.
* **Percentage**: `page.zen_mode.{horizontal|vertical}.percentage = u16`
Applies a percentage of the available space to the element

Converts the given percentage to a floating-point value and multiplies that with area. This
value is rounded back to a integer as part of the layout split calculation.

!!! note
As this value only accepts a u16, certain percentages that cannot be represented exactly
(e.g. 1/3) are not possible. You might want to use Ratio in such cases.

* **Ratio**: `page.zen_mode.{horizontal|vertical}.ratio = [u32, u32]`
Applies a ratio of the available space to the element

Converts the given ratio to a floating-point value and multiplies that with area. This value
is rounded back to a integer as part of the layout split calculation.



[release-0.9.0]: https://github.com/Builditluc/wiki-tui/releases/tag/v0.9
[release-0.5.1]: https://github.com/Builditluc/wiki-tui/releases/tag/v0.5.1
Expand Down
15 changes: 12 additions & 3 deletions src/components/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{collections::HashMap, sync::Arc};

use crossterm::event::{KeyCode, KeyEvent};
use ratatui::{
layout::{Constraint, Direction, Layout},
layout::{Constraint, Direction, Flex, Layout},
prelude::{Margin, Rect},
style::{Color, Modifier, Style, Stylize},
text::{Line, Span},
Expand Down Expand Up @@ -691,8 +691,17 @@ impl Component for PageComponent {
fn render(&mut self, f: &mut Frame, mut area: Rect) {
let zen_mode = self.config.page.zen_mode.clone();

// we add padding to the area by using a Block with padding
area = Block::new().padding(self.config.page.padding).inner(area);
// when in zen mode, use the constraints for the zen mode, otherwise the regular padding
area = if self.is_zen_mode() {
let [area] = Layout::horizontal([self.config.page.zen_horizontal])
.flex(Flex::Center)
.areas(area);
Layout::vertical([self.config.page.zen_vertical])
.flex(Flex::Center)
.split(area)[0]
} else {
Block::new().padding(self.config.page.padding).inner(area)
};

if !self.is_zen_mode || zen_mode.contains(ZenModeComponents::STATUS_BAR) {
area = self.render_status_bar(f, area);
Expand Down
38 changes: 37 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use anyhow::{bail, Context, Result};
use bitflags::bitflags;
use directories::ProjectDirs;
use ratatui::{
layout::Constraint,
style::{Color, Style},
widgets::{BorderType, Padding},
};
Expand Down Expand Up @@ -92,7 +93,10 @@ fn override_page_config(config: &mut PageConfig, user_config: UserPageConfig) {
if let Some(user_zen) = user_config.zen_mode {
override_options!(config, user_zen::{
default->default_zen,
include->zen_mode
include->zen_mode,

horizontal->zen_horizontal,
vertical->zen_vertical
});
}
}
Expand Down Expand Up @@ -122,6 +126,9 @@ pub struct PageConfig {

pub default_zen: bool,
pub zen_mode: ZenModeComponents,

pub zen_horizontal: Constraint,
pub zen_vertical: Constraint,
}

bitflags! {
Expand Down Expand Up @@ -202,6 +209,9 @@ impl Config {

default_zen: false,
zen_mode: ZenModeComponents::empty(),

zen_horizontal: Constraint::Percentage(80),
zen_vertical: Constraint::Percentage(90),
},
}
}
Expand Down Expand Up @@ -237,10 +247,36 @@ struct UserPageConfig {
zen_mode: Option<UserZenModeConfig>,
}

#[derive(Deserialize)]
#[serde(rename_all = "lowercase")]
enum UserConstraint {
Min(u16),
Max(u16),
Length(u16),
Percentage(u16),
Ratio(u32, u32),
}

#[allow(clippy::from_over_into)]
impl Into<Constraint> for UserConstraint {
fn into(self) -> Constraint {
match self {
UserConstraint::Min(u) => Constraint::Min(u),
UserConstraint::Max(u) => Constraint::Max(u),
UserConstraint::Length(u) => Constraint::Length(u),
UserConstraint::Percentage(u) => Constraint::Percentage(u),
UserConstraint::Ratio(u, v) => Constraint::Ratio(u, v),
}
}
}

#[derive(Deserialize)]
struct UserZenModeConfig {
default: Option<bool>,
include: Option<ZenModeComponents>,

horizontal: Option<UserConstraint>,
vertical: Option<UserConstraint>,
}

#[derive(Deserialize)]
Expand Down

0 comments on commit d713a9d

Please sign in to comment.