use std::rc::Rc;
use gpui_kit::{App, Bounds, Context, Modifiers, MouseButton, Pixels, Point, Window};
use super::{ColumnOf, KeyOf, LiveGridDelegate, LiveGridState, menu::GridTarget};
use crate::{
actions::OpenLink,
model::{CellValue, GridRow},
};
pub trait LinkOpener: 'static {
fn open(&self, url: &str, cx: &mut App);
}
#[derive(Clone, Copy, Debug, Default)]
pub struct OpenInBrowser;
impl LinkOpener for OpenInBrowser {
fn open(&self, url: &str, cx: &mut App) {
cx.open_url(url);
}
}
pub(super) struct LinkSlot {
opener: Rc<dyn LinkOpener>,
pub(super) rects: Vec<Bounds<Pixels>>,
pub(super) hovered: bool,
pub(super) series_on_link: bool,
}
impl Default for LinkSlot {
fn default() -> Self {
Self {
opener: Rc::new(OpenInBrowser),
rects: Vec::new(),
hovered: false,
series_on_link: false,
}
}
}
impl LinkSlot {
pub(super) fn is_over(&self, position: Point<Pixels>) -> bool {
self.rects.iter().any(|rect| rect.contains(&position))
}
}
impl<D: LiveGridDelegate> LiveGridState<D> {
pub fn with_link_opener(mut self, opener: impl LinkOpener) -> Self {
self.links.opener = Rc::new(opener);
self
}
pub fn is_link_hovered(&self) -> bool {
self.links.hovered
}
pub(super) fn is_pointer_on_link(&self) -> bool {
self.tooltip
.position
.is_some_and(|position| self.links.is_over(position))
}
pub(super) fn update_link_hover(
&mut self,
position: Option<Point<Pixels>>,
cx: &mut Context<Self>,
) {
let hovered = position.is_some_and(|position| self.links.is_over(position));
if hovered != self.links.hovered {
self.links.hovered = hovered;
cx.notify();
}
}
fn link_url(&self, target: &GridTarget<KeyOf<D>, ColumnOf<D>>) -> Option<String> {
let (row, column) = match target {
GridTarget::Cell { key, column } => (self.snapshot.get(key)?.clone(), *column),
GridTarget::Pinned { edge, key, column } => (
self.snapshot
.pinned_rows(*edge)
.iter()
.find(|row| row.key() == key)?
.clone(),
*column,
),
_ => return None,
};
match row.value(column) {
CellValue::Link(link) => Some(link.url().to_owned()),
_ => None,
}
}
fn open_link_of(
&self,
target: &GridTarget<KeyOf<D>, ColumnOf<D>>,
cx: &mut Context<Self>,
) -> bool {
let Some(url) = self.link_url(target) else {
return false;
};
let opener = self.links.opener.clone();
cx.defer(move |cx| opener.open(&url, cx));
true
}
pub(super) fn open_clicked_link(
&self,
target: &GridTarget<KeyOf<D>, ColumnOf<D>>,
button: MouseButton,
modifiers: Modifiers,
click_count: usize,
origin: Point<Pixels>,
cx: &mut Context<Self>,
) {
if button == MouseButton::Left
&& click_count == 1
&& !modifiers.modified()
&& self.links.is_over(origin)
{
self.open_link_of(target, cx);
}
}
pub(super) fn is_link_focused(&self) -> bool {
self.focused_link_target()
.is_some_and(|target| self.link_url(&target).is_some())
}
fn focused_link_target(&self) -> Option<GridTarget<KeyOf<D>, ColumnOf<D>>> {
let focus = self.selection.focus()?;
Some(GridTarget::Cell {
key: focus.row()?.clone(),
column: focus.column(),
})
}
pub(super) fn bind_link_actions<E: gpui_kit::InteractiveElement>(
root: E,
cx: &Context<Self>,
) -> E {
root.on_action(cx.listener(Self::on_open_link))
}
fn on_open_link(&mut self, _: &OpenLink, _: &mut Window, cx: &mut Context<Self>) {
let opened = self
.focused_link_target()
.is_some_and(|target| self.open_link_of(&target, cx));
if !opened {
cx.propagate();
}
}
}