Skip to content

Commit

Permalink
Add WebView (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
huacnlee authored Jul 18, 2024
1 parent 91e123e commit f614622
Show file tree
Hide file tree
Showing 14 changed files with 997 additions and 82 deletions.
708 changes: 647 additions & 61 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ default-members = ["crates/app"]
resolver = "2"

[workspace.dependencies]
# gpui = { path = "/Users/jason/github/zed/crates/gpui" }
gpui = { git = "https://github.com/zed-industries/zed.git" }
gpui = { git = "https://github.com/huacnlee/zed.git", branch = "export-platform-window" }
ui = { path = "crates/ui" }
story = { path = "crates/story" }
workspace = { path = "crates/workspace" }
Expand Down
4 changes: 4 additions & 0 deletions assets/icons/arrow-left.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions assets/icons/arrow-right.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions assets/icons/chevron-left.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions assets/icons/chevron-right.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 19 additions & 9 deletions crates/app/src/story_workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use prelude::FluentBuilder as _;
use story::{
ButtonStory, CheckboxStory, DropdownStory, ImageStory, InputStory, ListStory, PickerStory,
PopoverStory, ProgressStory, ResizableStory, ScrollableStory, StoryContainer, SwitchStory,
TableStory, TooltipStory,
TableStory, TooltipStory, WebViewStory,
};
use workspace::{dock::DockPosition, TitleBar, Workspace};

Expand Down Expand Up @@ -50,21 +50,23 @@ impl StoryWorkspace {
)
.detach();

StoryContainer::add_panel(
StoryContainer::add_pane(
"Input",
"A control that allows the user to input text.",
InputStory::view(cx).into(),
workspace.clone(),
DockPosition::Right,
px(350.0),
cx,
);
)
.detach();

StoryContainer::add_panel(
StoryContainer::add_pane(
"Checkbox",
"A control that allows the user to toggle between checked and not checked.",
CheckboxStory::view(cx).into(),
workspace.clone(),
DockPosition::Bottom,
px(200.),
cx,
);
)
.detach();

StoryContainer::add_pane(
"Switch",
Expand Down Expand Up @@ -128,6 +130,14 @@ impl StoryWorkspace {
)
.detach();

StoryContainer::add_panel(
WebViewStory::view(cx).into(),
workspace.clone(),
DockPosition::Right,
px(450.),
cx,
);

StoryContainer::add_pane(
"Table",
"Powerful table and datagrids built.",
Expand Down
4 changes: 4 additions & 0 deletions crates/story/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ anyhow = "1"
workspace.workspace = true
charts-rs = "0.3"
image = "0.25.1"
wry = "0"

[lints]
workspace = true
12 changes: 3 additions & 9 deletions crates/story/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod scrollable_story;
mod switch_story;
mod table_story;
mod tooltip_story;
mod webview_story;

pub use button_story::ButtonStory;
pub use checkbox_story::CheckboxStory;
Expand All @@ -27,6 +28,7 @@ pub use scrollable_story::ScrollableStory;
pub use switch_story::SwitchStory;
pub use table_story::TableStory;
pub use tooltip_story::TooltipStory;
pub use webview_story::WebViewStory;

use gpui::{
div, prelude::FluentBuilder as _, px, AnyElement, AnyView, AppContext, Div, EventEmitter,
Expand All @@ -46,14 +48,6 @@ pub fn init(cx: &mut AppContext) {
input_story::init(cx);
}

pub fn story_case(
name: &'static str,
description: &'static str,
cx: &mut WindowContext,
) -> StoryContainer {
StoryContainer::new(name, description, cx)
}

pub fn section(title: impl Into<SharedString>, cx: &WindowContext) -> Div {
use ui::theme::ActiveTheme;
let theme = cx.theme();
Expand Down Expand Up @@ -104,7 +98,7 @@ impl Item for StoryContainer {
Label::new(self.name.clone()).into_any_element()
}

fn deactivated(&mut self, _: &mut ViewContext<Self>) {
fn deactivated(&mut self, _cx: &mut ViewContext<Self>) {
self.active = false;
}

Expand Down
105 changes: 105 additions & 0 deletions crates/story/src/webview_story.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
use gpui::{
div, ClickEvent, FocusHandle, FocusableView, ParentElement as _, Render, Styled as _, View,
ViewContext, VisualContext as _, WindowContext,
};
use ui::{
button::Button,
h_flex,
input::{TextEvent, TextInput},
theme::ActiveTheme,
v_flex,
webview::WebView,
Clickable, IconName,
};

pub struct WebViewStory {
focus_handle: FocusHandle,
webview: View<WebView>,
address_input: View<TextInput>,
}

impl WebViewStory {
pub fn view(cx: &mut WindowContext) -> View<Self> {
let focus_handle = cx.focus_handle();

let webview = cx.new_view(|cx| WebView::new(cx));

let address_input = cx.new_view(|cx| {
let mut input = TextInput::new(cx);
input.set_text("https://github.com/explore", cx);
input
});

let url = address_input.read(cx).text();
webview.update(cx, |view, _| {
view.load_url(&url);
});

cx.new_view(|cx| {
let this = WebViewStory {
focus_handle,
webview,
address_input: address_input.clone(),
};

cx.subscribe(
&address_input,
|this: &mut Self, input, event: &TextEvent, cx| match event {
TextEvent::PressEnter => {
let url = input.read(cx).text();
this.webview.update(cx, |view, _| {
view.load_url(&url);
});
}
_ => {}
},
)
.detach();

this
})
}

pub fn hide(&self, cx: &mut WindowContext) {
self.webview.update(cx, |webview, _| webview.hide())
}

fn go_back(&mut self, _: &ClickEvent, cx: &mut ViewContext<Self>) {
self.webview.update(cx, |webview, _| {
webview.back().unwrap();
});
}
}

impl FocusableView for WebViewStory {
fn focus_handle(&self, _cx: &gpui::AppContext) -> FocusHandle {
self.focus_handle.clone()
}
}

impl Render for WebViewStory {
fn render(&mut self, cx: &mut gpui::ViewContext<Self>) -> impl gpui::IntoElement {
v_flex()
.p_2()
.gap_3()
.size_full()
.child(
h_flex()
.gap_2()
.items_center()
.child(
Button::new("go-back", cx)
.icon(IconName::ArrowLeft)
.on_click(cx.listener(Self::go_back)),
)
.child(self.address_input.clone()),
)
.child(
div()
.size_full()
.border_1()
.border_color(cx.theme().border)
.child(self.webview.clone()),
)
}
}
8 changes: 7 additions & 1 deletion crates/ui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ anyhow = "1"
log = "0.4"
serde = "1.0.203"
serde_json = "1"
wry = "0"

smallvec = "1.13.2"
windows = "0.57.0"
unicode-segmentation = "1.11.0"
Expand All @@ -24,6 +24,12 @@ usvg = { version = "0.41.0", default-features = false }
taffy = "0.4.3"
paste = "1"
once_cell = "1.19.0"
raw-window-handle = "0.6.2"
winit = "0.30.3"
objc = "0.2"
objc_id = "0.1"
cocoa = "0.25"
wry = "0"

[lints]
workspace = true
8 changes: 8 additions & 0 deletions crates/ui/src/icon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ pub enum IconName {
CircleX,
Loader,
LoaderCircle,
ArrowLeft,
ArrowRight,
ChevronLeft,
ChevronRight,
}

impl IconName {
Expand All @@ -45,6 +49,10 @@ impl IconName {
IconName::CircleX => "icons/circle-x.svg",
IconName::Loader => "icons/loader.svg",
IconName::LoaderCircle => "icons/loader-circle.svg",
IconName::ArrowLeft => "icons/arrow-left.svg",
IconName::ArrowRight => "icons/arrow-right.svg",
IconName::ChevronLeft => "icons/chevron-left.svg",
IconName::ChevronRight => "icons/chevron-right.svg",
}
.into()
}
Expand Down
2 changes: 2 additions & 0 deletions crates/ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub mod tab;
pub mod table;
pub mod theme;
pub mod tooltip;
pub mod webview;

pub use clickable::Clickable;
pub use disableable::Disableable;
Expand All @@ -52,4 +53,5 @@ pub fn init(cx: &mut gpui::AppContext) {
popover::init(cx);
popup_menu::init(cx);
table::init(cx);
webview::init(cx)
}
Loading

0 comments on commit f614622

Please sign in to comment.