Skip to content

Commit

Permalink
feat!: add ui.Text, ui.Table, remove ui.Paragraph and `ui.ListI…
Browse files Browse the repository at this point in the history
…tem` (sxyazi#1776)
  • Loading branch information
sxyazi authored Oct 13, 2024
1 parent 35c3c9e commit 43b5ae0
Show file tree
Hide file tree
Showing 52 changed files with 466 additions and 273 deletions.
2 changes: 1 addition & 1 deletion yazi-core/src/manager/commands/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl Manager {
if let Some(ref p) = tab.parent {
to_watch.insert(&p.url);
}
if let Some(h) = tab.current.hovered().filter(|&h| h.is_dir()) {
if let Some(h) = tab.hovered().filter(|&h| h.is_dir()) {
to_watch.insert(&h.url);
}
}
Expand Down
4 changes: 2 additions & 2 deletions yazi-core/src/manager/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ yazi_macro::mod_flat!(
tab_switch
unyank
update_files
update_mimetype
update_mimes
update_paged
update_task
update_tasks
update_yanked
yank
);
2 changes: 1 addition & 1 deletion yazi-core/src/manager/commands/tab_create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl Tabs {

if !opt.current {
tab.cd(opt.url);
} else if let Some(h) = self.active().current.hovered() {
} else if let Some(h) = self.active().hovered() {
tab.conf = self.active().conf.clone();
tab.apply_files_attrs();
tab.reveal(h.url.to_regular());
Expand Down
4 changes: 2 additions & 2 deletions yazi-core/src/manager/commands/update_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl Manager {
Self::update_current(tab, op, tasks);
} else if matches!(&tab.parent, Some(p) if *url == p.url) {
Self::update_parent(tab, op);
} else if matches!(tab.current.hovered(), Some(h) if *url == h.url) {
} else if matches!(tab.hovered(), Some(h) if *url == h.url) {
Self::update_hovered(tab, op);
} else {
Self::update_history(tab, op);
Expand All @@ -74,7 +74,7 @@ impl Manager {
}

fn update_current(tab: &mut Tab, op: Cow<FilesOp>, tasks: &Tasks) {
let hovered = tab.current.hovered().filter(|_| tab.current.tracing).map(|h| h.urn_owned());
let hovered = tab.hovered().filter(|_| tab.current.tracing).map(|h| h.urn_owned());
let calc = !matches!(*op, FilesOp::Size(..) | FilesOp::Deleting(..));

let foreign = matches!(op, Cow::Borrowed(_));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ impl TryFrom<Cmd> for Opt {
}

impl Manager {
pub fn update_mimetype(&mut self, opt: impl TryInto<Opt>, tasks: &Tasks) {
pub fn update_mimes(&mut self, opt: impl TryInto<Opt>, tasks: &Tasks) {
let Ok(opt) = opt.try_into() else {
return error!("invalid arguments for update_mimetype");
return error!("invalid arguments for update_mimes");
};

let linked = LINKED.read();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ use yazi_shared::{event::Cmd, fs::Url};
use crate::manager::Manager;

pub struct Opt {
url: Url,
urls: Vec<Url>,
}

impl TryFrom<Cmd> for Opt {
type Error = ();

fn try_from(mut c: Cmd) -> Result<Self, Self::Error> {
Ok(Self { url: c.take_any("url").ok_or(())? })
Ok(Self { urls: c.take_any("urls").ok_or(())? })
}
}

impl Manager {
pub fn update_task(&mut self, opt: impl TryInto<Opt>) {
pub fn update_tasks(&mut self, opt: impl TryInto<Opt>) {
let Ok(opt) = opt.try_into() else {
return;
};

self.watcher.push_file(opt.url);
self.watcher.push_files(opt.urls);
}
}
2 changes: 1 addition & 1 deletion yazi-core/src/manager/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl Manager {
pub fn parent(&self) -> Option<&Folder> { self.active().parent.as_ref() }

#[inline]
pub fn hovered(&self) -> Option<&File> { self.active().current.hovered() }
pub fn hovered(&self) -> Option<&File> { self.active().hovered() }

#[inline]
pub fn hovered_folder(&self) -> Option<&Folder> { self.active().hovered_folder() }
Expand Down
9 changes: 6 additions & 3 deletions yazi-core/src/manager/watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,12 @@ impl Watcher {
self.in_tx.send(new.into_iter().cloned().collect()).ok();
}

pub(super) fn push_file(&self, url: Url) {
if url.parent_url().is_some_and(|p| WATCHED.read().contains(&p)) {
self.out_tx.send(url).ok();
pub(super) fn push_files(&self, url: Vec<Url>) {
let watched = WATCHED.read();
for u in url {
if u.parent_url().is_some_and(|p| watched.contains(&p)) {
self.out_tx.send(u).ok();
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion yazi-core/src/tab/commands/enter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ use crate::tab::Tab;

impl Tab {
pub fn enter(&mut self, _: Cmd) {
self.current.hovered().filter(|h| h.is_dir()).map(|h| h.url.to_regular()).map(|u| self.cd(u));
self.hovered().filter(|h| h.is_dir()).map(|h| h.url.to_regular()).map(|u| self.cd(u));
}
}
2 changes: 1 addition & 1 deletion yazi-core/src/tab/commands/escape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl Tab {
}

self.selected.clear();
if self.current.hovered().is_some_and(|h| h.is_dir()) {
if self.hovered().is_some_and(|h| h.is_dir()) {
ManagerProxy::peek(true);
}
render_and!(true)
Expand Down
4 changes: 2 additions & 2 deletions yazi-core/src/tab/commands/filter_do.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ impl Tab {
ManagerProxy::update_paged(); // Update for paged files in next loop
}

let hovered = self.current.hovered().map(|f| f.urn_owned());
let hovered = self.hovered().map(|f| f.urn_owned());
if !self.current.files.set_filter(filter) {
return;
}

self.current.repos(hovered.as_ref().map(|u| u.as_urn()));
if self.current.hovered().map(|f| f.urn()) != hovered.as_ref().map(|u| u.as_urn()) {
if self.hovered().map(|f| f.urn()) != hovered.as_ref().map(|u| u.as_urn()) {
ManagerProxy::hover(None, self.idx);
}

Expand Down
6 changes: 3 additions & 3 deletions yazi-core/src/tab/commands/hidden.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ impl Tab {
_ => !self.conf.show_hidden,
};

let hovered = self.current.hovered().map(|f| f.url_owned());
let hovered = self.hovered().map(|f| f.url_owned());
self.apply_files_attrs();

if hovered.as_ref() != self.current.hovered().map(|f| &f.url) {
if hovered.as_ref() != self.hovered().map(|f| &f.url) {
ManagerProxy::hover(hovered, self.idx);
} else if self.current.hovered().is_some_and(|f| f.is_dir()) {
} else if self.hovered().is_some_and(|f| f.is_dir()) {
ManagerProxy::peek(true);
}
ManagerProxy::update_paged();
Expand Down
4 changes: 3 additions & 1 deletion yazi-core/src/tab/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ yazi_macro::mod_flat!(
hidden
leave
linemode
preview
reveal
search
select
select_all
shell
sort
spot
toggle
toggle_all
update_peeked
update_spotted
visual_mode
);
20 changes: 20 additions & 0 deletions yazi-core/src/tab/commands/spot.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use yazi_shared::event::{Cmd, Data};

use crate::tab::Tab;

struct Opt {
skip: Option<usize>,
}

impl From<Cmd> for Opt {
fn from(c: Cmd) -> Self { Self { skip: c.first().and_then(Data::as_usize) } }
}

impl Tab {
#[yazi_codegen::command]
pub fn spot(&mut self, c: Cmd) {
let Some(hovered) = self.hovered().cloned() else {
return self.preview.reset();
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ impl TryFrom<Cmd> for Opt {
}

impl Tab {
pub fn preview(&mut self, opt: impl TryInto<Opt>) {
let Some(hovered) = self.current.hovered().map(|h| &h.url) else {
pub fn update_peeked(&mut self, opt: impl TryInto<Opt>) {
let Some(hovered) = self.hovered().map(|h| &h.url) else {
return self.preview.reset();
};

Expand Down
22 changes: 22 additions & 0 deletions yazi-core/src/tab/commands/update_spotted.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use yazi_plugin::utils::PreviewLock;
use yazi_shared::event::Cmd;

use crate::tab::Tab;

pub struct Opt {
lock: PreviewLock,
}

impl TryFrom<Cmd> for Opt {
type Error = ();

fn try_from(mut c: Cmd) -> Result<Self, Self::Error> {
Ok(Self { lock: c.take_any("lock").ok_or(())? })
}
}

impl Tab {
pub fn update_spotted(&mut self, opt: impl TryInto<Opt>) {
todo!();
}
}
13 changes: 8 additions & 5 deletions yazi-core/src/tab/tab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use yazi_adapter::Dimension;
use yazi_config::{LAYOUT, popup::{Origin, Position}};
use yazi_fs::{Folder, FolderStage};
use yazi_macro::render;
use yazi_shared::fs::Url;
use yazi_shared::fs::{File, Url};

use super::{Backstack, Config, Finder, History, Mode, Preview};
use crate::tab::Selected;
Expand Down Expand Up @@ -42,8 +42,11 @@ impl Tab {
#[inline]
pub fn cwd(&self) -> &Url { &self.current.url }

#[inline]
pub fn hovered(&self) -> Option<&File> { self.current.hovered() }

pub fn hovered_rect(&self) -> Option<Rect> {
let y = self.current.files.position(self.current.hovered()?.urn())? - self.current.offset;
let y = self.current.files.position(self.hovered()?.urn())? - self.current.offset;

let mut rect = LAYOUT.load().current;
rect.y = rect.y.saturating_sub(1) + y as u16;
Expand All @@ -62,7 +65,7 @@ impl Tab {

pub fn selected_or_hovered(&self, reorder: bool) -> Box<dyn Iterator<Item = &Url> + '_> {
if self.selected.is_empty() {
Box::new(self.current.hovered().map(|h| vec![&h.url]).unwrap_or_default().into_iter())
Box::new(self.hovered().map(|h| vec![&h.url]).unwrap_or_default().into_iter())
} else if !reorder {
Box::new(self.selected.keys())
} else {
Expand All @@ -73,7 +76,7 @@ impl Tab {
}

pub fn hovered_and_selected(&self, reorder: bool) -> Box<dyn Iterator<Item = &Url> + '_> {
let Some(h) = self.current.hovered() else { return Box::new(iter::empty()) };
let Some(h) = self.hovered() else { return Box::new(iter::empty()) };

if self.selected.is_empty() {
Box::new([&h.url, &h.url].into_iter())
Expand All @@ -89,7 +92,7 @@ impl Tab {
// --- History
#[inline]
pub fn hovered_folder(&self) -> Option<&Folder> {
self.current.hovered().filter(|&h| h.is_dir()).and_then(|h| self.history.get(&h.url))
self.hovered().filter(|&h| h.is_dir()).and_then(|h| self.history.get(&h.url))
}

pub fn apply_files_attrs(&mut self) {
Expand Down
8 changes: 5 additions & 3 deletions yazi-fm/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,22 @@ impl<'a> Executor<'a> {
};
}

on!(MANAGER, update_task);
on!(MANAGER, update_tasks);
on!(MANAGER, update_files, &self.app.cx.tasks);
on!(MANAGER, update_mimetype, &self.app.cx.tasks);
on!(MANAGER, update_mimes, &self.app.cx.tasks);
on!(MANAGER, update_paged, &self.app.cx.tasks);
on!(MANAGER, update_yanked);
on!(MANAGER, hover);
on!(MANAGER, peek);
on!(MANAGER, seek);
on!(ACTIVE, spot);
on!(MANAGER, refresh, &self.app.cx.tasks);
on!(MANAGER, quit, &self.app.cx.tasks);
on!(MANAGER, close, &self.app.cx.tasks);
on!(MANAGER, suspend);
on!(ACTIVE, escape);
on!(ACTIVE, preview);
on!(ACTIVE, update_peeked);
on!(ACTIVE, update_spotted);

// Navigation
on!(ACTIVE, arrow);
Expand Down
2 changes: 1 addition & 1 deletion yazi-fm/src/lives/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl File {
});
reg.add_method("in_current", |_, me, ()| Ok(me.folder().url == me.tab().current.url));
reg.add_method("in_preview", |_, me, ()| {
Ok(me.tab().current.hovered().is_some_and(|f| f.url == me.folder().url))
Ok(me.tab().hovered().is_some_and(|f| f.url == me.folder().url))
});
reg.add_method("found", |lua, me, ()| {
let cx = lua.named_registry_value::<CtxRef>("cx")?;
Expand Down
10 changes: 4 additions & 6 deletions yazi-plugin/preset/components/current.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function Current:empty()
end

return {
ui.Paragraph(self._area, { line }):align(ui.Paragraph.CENTER),
ui.Text(line):area(self._area):align(ui.Text.CENTER),
}
end

Expand All @@ -32,14 +32,12 @@ function Current:render()
local entities, linemodes = {}, {}
for _, f in ipairs(files) do
linemodes[#linemodes + 1] = Linemode:new(f):render()

local entity = Entity:new(f)
entities[#entities + 1] = ui.ListItem(entity:render()):style(entity:style())
entities[#entities + 1] = Entity:new(f):render()
end

return {
ui.List(self._area, entities),
ui.Paragraph(self._area, linemodes):align(ui.Paragraph.RIGHT),
ui.List(entities):area(self._area),
ui.Text(linemodes):area(self._area):align(ui.Text.RIGHT),
}
end

Expand Down
2 changes: 1 addition & 1 deletion yazi-plugin/preset/components/entity.lua
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function Entity:render()
for _, c in ipairs(self._children) do
lines[#lines + 1] = (type(c[1]) == "string" and self[c[1]] or c[1])(self)
end
return ui.Line(lines)
return ui.Line(lines):style(self:style())
end

function Entity:style()
Expand Down
4 changes: 2 additions & 2 deletions yazi-plugin/preset/components/header.lua
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ function Header:render()

local left = self:children_render(self.LEFT)
return {
ui.Paragraph(self._area, { left }),
ui.Paragraph(self._area, { right }):align(ui.Paragraph.RIGHT),
ui.Text(left):area(self._area),
ui.Text(right):area(self._area):align(ui.Text.RIGHT),
}
end

Expand Down
5 changes: 2 additions & 3 deletions yazi-plugin/preset/components/parent.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ function Parent:render()

local items = {}
for _, f in ipairs(self._folder.window) do
local entity = Entity:new(f)
items[#items + 1] = ui.ListItem(entity:render()):style(entity:style())
items[#items + 1] = Entity:new(f):render()
end

return {
ui.List(self._area, items),
ui.List(items):area(self._area),
}
end

Expand Down
2 changes: 1 addition & 1 deletion yazi-plugin/preset/components/progress.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ end
function Progress:partial_render()
local progress = cx.tasks.progress
if progress.total == 0 then
return { ui.Paragraph(self._area, {}) }
return { ui.Text {} }
end

local gauge = ui.Gauge(self._area)
Expand Down
Loading

0 comments on commit 43b5ae0

Please sign in to comment.