Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Harzu committed Oct 5, 2024
1 parent a70e9df commit 8c180cd
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 148 deletions.
3 changes: 2 additions & 1 deletion examples/custom_bindings/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use iced::font::{Family, Stretch, Weight};
use iced::keyboard::Modifiers;
use iced::widget::container;
use iced::{window, Element, Font, Length, Size, Subscription, Task, Theme};
use iced_term::TerminalView;
use iced_term::{
self,
bindings::{Binding, BindingAction, InputKind, KeyboardBinding},
Expand Down Expand Up @@ -136,7 +137,7 @@ impl App {
}

fn view(&self) -> Element<Event, Theme, iced::Renderer> {
container(iced_term::term_view(&self.term).map(Event::Terminal))
container(TerminalView::new(&self.term).map(Event::Terminal))
.width(Length::Fill)
.height(Length::Fill)
.into()
Expand Down
3 changes: 2 additions & 1 deletion examples/fonts/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use iced::advanced::graphics::core::Element;
use iced::font::{Family, Stretch, Weight};
use iced::widget::{button, column, container, row};
use iced::{window, Font, Length, Size, Subscription, Task, Theme};
use iced_term::TerminalView;

const TERM_FONT_JET_BRAINS_BYTES: &[u8] = include_bytes!(
"../assets/fonts/JetBrains/JetBrainsMonoNerdFontMono-Bold.ttf"
Expand Down Expand Up @@ -160,7 +161,7 @@ impl App {
.padding(8)
.on_press(Event::FontSizeDec),
],
row![iced_term::term_view(&self.term).map(Event::Terminal)],
row![TerminalView::new(&self.term).map(Event::Terminal)],
];

container(content)
Expand Down
3 changes: 2 additions & 1 deletion examples/full_screen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use iced::advanced::graphics::core::Element;
use iced::font::{Family, Stretch, Weight};
use iced::widget::container;
use iced::{window, Font, Length, Size, Subscription, Task, Theme};
use iced_term::TerminalView;

fn main() -> iced::Result {
iced::application(App::title, App::update, App::view)
Expand Down Expand Up @@ -85,7 +86,7 @@ impl App {
}

fn view(&self) -> Element<Event, Theme, iced::Renderer> {
container(iced_term::term_view(&self.term).map(Event::Terminal))
container(TerminalView::new(&self.term).map(Event::Terminal))
.width(Length::Fill)
.height(Length::Fill)
.into()
Expand Down
10 changes: 5 additions & 5 deletions examples/split_view/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use iced::widget::{button, container, responsive, row, text};
use iced::Task;
use iced::{alignment, Font};
use iced::{window, Color, Element, Length, Size, Subscription};
use iced_term::{term_view, TermView};
use iced_term::TerminalView;
use std::collections::HashMap;

const TERM_FONT_JET_BRAINS_BYTES: &[u8] = include_bytes!(
Expand Down Expand Up @@ -95,7 +95,7 @@ impl App {
self.panes_created as u64,
self.term_settings.clone(),
);
let command = TermView::focus(tab.widget_id());
let command = TerminalView::focus(tab.widget_id());
self.tabs.insert(self.panes_created as u64, tab);

if let Some((pane, _)) = result {
Expand All @@ -111,7 +111,7 @@ impl App {
self.tabs.get_mut(&(new_focused_pane.id as u64)).unwrap();

self.focus = Some(pane);
return TermView::focus(new_focused_tab.widget_id());
return TerminalView::focus(new_focused_tab.widget_id());
},
Event::Resized(pane_grid::ResizeEvent { split, ratio }) => {
self.panes.resize(split, ratio);
Expand All @@ -128,7 +128,7 @@ impl App {
.get_mut(&(new_focused_pane.id as u64))
.unwrap();

return TermView::focus(new_focused_tab.widget_id());
return TerminalView::focus(new_focused_tab.widget_id());
} else {
return window::get_latest().and_then(window::close);
}
Expand Down Expand Up @@ -241,7 +241,7 @@ fn view_content(
tabs: &HashMap<u64, iced_term::Terminal>,
) -> Element<'_, Event> {
let tab = tabs.get(&pane_id).expect("tab with target id not found");
container(term_view(tab).map(Event::Terminal))
container(TerminalView::new(tab).map(Event::Terminal))
.width(Length::Fill)
.height(Length::Fill)
.padding(5)
Expand Down
3 changes: 2 additions & 1 deletion examples/themes/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use iced::advanced::graphics::core::Element;
use iced::font::{Family, Stretch, Weight};
use iced::widget::{button, column, container, row};
use iced::{window, Font, Length, Size, Subscription, Task, Theme};
use iced_term::TerminalView;

const TERM_FONT_JET_BRAINS_BYTES: &[u8] = include_bytes!(
"../assets/fonts/JetBrains/JetBrainsMonoNerdFontMono-Bold.ttf"
Expand Down Expand Up @@ -150,7 +151,7 @@ impl App {
}))
),
],
row![iced_term::term_view(&self.term).map(Event::Terminal)]
row![TerminalView::new(&self.term).map(Event::Terminal)]
];

container(content)
Expand Down
8 changes: 4 additions & 4 deletions src/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ macro_rules! generate_bindings {
}

#[derive(Clone, Debug)]
pub struct BindingsLayout {
pub(crate) struct BindingsLayout {
layout: Vec<(Binding<InputKind>, BindingAction)>,
}

Expand All @@ -93,7 +93,7 @@ impl Default for BindingsLayout {
}

impl BindingsLayout {
pub fn new() -> Self {
pub(crate) fn new() -> Self {
let mut layout = Self {
layout: default_keyboard_bindings(),
};
Expand All @@ -102,7 +102,7 @@ impl BindingsLayout {
layout
}

pub fn add_bindings(
pub(crate) fn add_bindings(
&mut self,
bindings: Vec<(Binding<InputKind>, BindingAction)>,
) {
Expand All @@ -118,7 +118,7 @@ impl BindingsLayout {
}
}

pub fn get_action(
pub(crate) fn get_action(
&self,
input: InputKind,
modifiers: Modifiers,
Expand Down
24 changes: 4 additions & 20 deletions src/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ use iced_graphics::text::paragraph;

#[derive(Debug, Clone)]
pub struct TermFont {
size: f32,
font_type: Font,
scale_factor: f32,
measure: Size<f32>,
pub(crate) size: f32,
pub(crate) font_type: Font,
pub(crate) scale_factor: f32,
pub(crate) measure: Size<f32>,
}

impl TermFont {
Expand All @@ -28,22 +28,6 @@ impl TermFont {
),
}
}

pub fn size(&self) -> f32 {
self.size
}

pub fn font_type(&self) -> Font {
self.font_type
}

pub fn measure(&self) -> Size<f32> {
self.measure
}

pub fn scale_factor(&self) -> f32 {
self.scale_factor
}
}

fn font_measure(
Expand Down
7 changes: 4 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
pub mod actions;
mod backend;
pub mod bindings;
mod font;
pub mod settings;

mod backend;
mod font;
mod subscription;
mod terminal;
mod theme;
Expand All @@ -12,4 +13,4 @@ pub use alacritty_terminal::term::TermMode;
pub use subscription::Subscription;
pub use terminal::{Command, Event, Terminal};
pub use theme::{ColorPalette, Theme};
pub use view::{term_view, TermView, TermViewState};
pub use view::TerminalView;
6 changes: 5 additions & 1 deletion src/settings.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use iced::Font;

use crate::ColorPalette;

#[cfg(target_os = "windows")]
const DEFAULT_SHELL: &str = "wsl.exe";

#[cfg(not(target_os = "windows"))]
const DEFAULT_SHELL: &str = "/bin/bash";


#[derive(Default, Clone)]
pub struct Settings {
pub font: FontSettings,
Expand Down
6 changes: 3 additions & 3 deletions src/terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ pub enum Command {

pub struct Terminal {
pub id: u64,
backend_settings: BackendSettings,
pub(crate) font: TermFont,
pub(crate) theme: Theme,
pub(crate) cache: Cache,
pub(crate) bindings: BindingsLayout,
pub(crate) backend: Option<Backend>,
backend_settings: BackendSettings,
}

impl Terminal {
Expand Down Expand Up @@ -57,7 +57,7 @@ impl Terminal {
self.id,
sender,
self.backend_settings.clone(),
self.font.measure(),
self.font.measure,
)
.unwrap_or_else(|_| {
panic!("init pty with ID: {} is failed", self.id);
Expand All @@ -74,7 +74,7 @@ impl Terminal {
if let Some(ref mut backend) = self.backend {
action = backend.process_command(BackendCommand::Resize(
None,
Some(self.font.measure()),
Some(self.font.measure),
));
if action == Action::Redraw {
self.redraw();
Expand Down
84 changes: 45 additions & 39 deletions src/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ use alacritty_terminal::vte::ansi::{self, NamedColor};
use iced::{widget::container, Color};
use std::collections::HashMap;

pub(crate) trait TerminalStyle {
fn container_style(&self) -> container::Style;
}

#[derive(Debug, Clone)]
pub struct ColorPalette {
pub foreground: String,
Expand Down Expand Up @@ -80,7 +84,7 @@ impl Default for Theme {
fn default() -> Self {
Self {
palette: Box::<ColorPalette>::default(),
ansi256_colors: Theme::get_ansi256_colors(),
ansi256_colors: build_ansi256_colors(),
}
}
}
Expand All @@ -89,23 +93,7 @@ impl Theme {
pub fn new(settings: ThemeSettings) -> Self {
Self {
palette: settings.color_pallete,
ansi256_colors: Theme::get_ansi256_colors(),
}
}

pub(crate) fn container_style(&self) -> container::Style {
container::Style {
background: Some(
hex_to_color(&self.palette.background)
.unwrap_or_else(|_| {
panic!(
"invalid background color {}",
self.palette.background
)
})
.into(),
),
..container::Style::default()
ansi256_colors: build_ansi256_colors(),
}
}

Expand Down Expand Up @@ -192,34 +180,34 @@ impl Theme {
},
}
}
}

fn get_ansi256_colors() -> HashMap<u8, Color> {
let mut ansi256_colors = HashMap::new();
fn build_ansi256_colors() -> HashMap<u8, Color> {
let mut ansi256_colors = HashMap::new();

for r in 0..6 {
for g in 0..6 {
for b in 0..6 {
// Reserve the first 16 colors for config.
let index = 16 + r * 36 + g * 6 + b;
let color = Color::from_rgb8(
if r == 0 { 0 } else { r * 40 + 55 },
if g == 0 { 0 } else { g * 40 + 55 },
if b == 0 { 0 } else { b * 40 + 55 },
);
ansi256_colors.insert(index, color);
}
for r in 0..6 {
for g in 0..6 {
for b in 0..6 {
// Reserve the first 16 colors for config.
let index = 16 + r * 36 + g * 6 + b;
let color = Color::from_rgb8(
if r == 0 { 0 } else { r * 40 + 55 },
if g == 0 { 0 } else { g * 40 + 55 },
if b == 0 { 0 } else { b * 40 + 55 },
);
ansi256_colors.insert(index, color);
}
}
}

let index: u8 = 232;
for i in 0..24 {
let value = i * 10 + 8;
ansi256_colors
.insert(index + i, Color::from_rgb8(value, value, value));
}

let index: u8 = 232;
for i in 0..24 {
let value = i * 10 + 8;
ansi256_colors
.insert(index + i, Color::from_rgb8(value, value, value));
}

ansi256_colors
}

fn hex_to_color(hex: &str) -> anyhow::Result<Color> {
Expand All @@ -234,6 +222,24 @@ fn hex_to_color(hex: &str) -> anyhow::Result<Color> {
Ok(Color::from_rgb8(r, g, b))
}

impl TerminalStyle for Theme {
fn container_style(&self) -> container::Style {
container::Style {
background: Some(
hex_to_color(&self.palette.background)
.unwrap_or_else(|_| {
panic!(
"invalid background color {}",
self.palette.background
)
})
.into(),
),
..container::Style::default()
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
Loading

0 comments on commit 8c180cd

Please sign in to comment.