Skip to content

Draft: Out-of-box Experience #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 59 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions assets/brand/reticle.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/brand/scope-login-bg.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions src/ui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ random-string = "1.1.0"
rust-embed = "8.5.0"
chrono.workspace = true
catty = "0.1.5"
directories = "5.0.1"
clap = { version = "4.5.21", features = ["derive"] }

[features]
default = ["gpui/x11"]
Expand Down
11 changes: 7 additions & 4 deletions src/ui/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,30 @@ use components::theme::ActiveTheme;
use gpui::{div, img, rgb, Context, Model, ParentElement, Render, Styled, View, ViewContext, VisualContext};
use scope_backend_discord::{channel::DiscordChannel, client::DiscordClient, snowflake::Snowflake};

use crate::channel::ChannelView;
use crate::{app_state::StateModel, channel::ChannelView};

pub struct App {
channel: Model<Option<View<ChannelView<DiscordChannel>>>>,
}

impl App {
pub fn new(ctx: &mut ViewContext<'_, Self>) -> App {
let token = dotenv::var("DISCORD_TOKEN").expect("Must provide DISCORD_TOKEN in .env");
let demo_channel_id = dotenv::var("DEMO_CHANNEL_ID").expect("Must provide DEMO_CHANNEL_ID in .env");
let demo_channel_id = dotenv::var("DEMO_CHANNEL_ID").unwrap_or("1306357873437179944".to_owned());

let mut context = ctx.to_async();

let channel = ctx.new_model(|_| None);

let async_channel = channel.clone();

let mut async_ctx = ctx.to_async();

ctx
.foreground_executor()
.spawn(async move {
let client = DiscordClient::new(token).await;
let token = async_ctx.update_global::<StateModel, Option<String>>(|global, cx| global.take_token(&mut *cx)).unwrap();

let client = DiscordClient::new(token.expect("Token to be set")).await;

let channel = DiscordChannel::new(
client.clone(),
Expand Down
44 changes: 36 additions & 8 deletions src/ui/src/app_state.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,43 @@
use std::sync::Weak;
use gpui::*;

use gpui::{AppContext, Global};
#[derive(Clone)]
pub struct State {
pub token: Option<String>,
}

pub struct AppState {}
#[derive(Clone)]
pub struct StateModel {
pub inner: Model<State>,
}

struct GlobalAppState();
impl StateModel {
pub fn init(cx: &mut AppContext) {
let model = cx.new_model(|_cx| State { token: None });
let this = Self { inner: model };
cx.set_global(this.clone());
}

impl Global for GlobalAppState {}
pub fn update(f: impl FnOnce(&mut Self, &mut AppContext), cx: &mut AppContext) {
if !cx.has_global::<Self>() {
return;
}
cx.update_global::<Self, _>(|mut this, cx| {
f(&mut this, cx);
});
}

impl AppState {
pub fn set_global(_app_state: Weak<AppState>, cx: &mut AppContext) {
cx.set_global(GlobalAppState());
pub fn provide_token(&self, token: String, cx: &mut AppContext) {
self.inner.update(cx, |model, _| model.token = Some(token))
}

pub fn take_token(&self, cx: &mut AppContext) -> Option<String> {
self.inner.update(cx, |model, _| model.token.take())
}
}

impl Global for StateModel {}

#[derive(Clone, Debug)]
pub struct ListChangedEvent {}

impl EventEmitter<ListChangedEvent> for State {}
58 changes: 39 additions & 19 deletions src/ui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ pub mod app;
pub mod app_state;
pub mod channel;
pub mod menu;
pub mod oobe;

use std::sync::Arc;

use app_state::AppState;
use app_state::StateModel;
use clap::Parser;
use components::theme::{hsl, Theme, ThemeColor, ThemeMode};
use gpui::*;
use http_client::anyhow;
use menu::app_menus;
use oobe::login::OobeTokenLogin;

#[derive(rust_embed::RustEmbed)]
#[folder = "../../assets"]
Expand All @@ -26,7 +29,8 @@ impl AssetSource for Assets {
}
}

fn init(_: Arc<AppState>, cx: &mut AppContext) -> Result<()> {
fn init(cx: &mut AppContext) -> Result<()> {
StateModel::init(cx);
components::init(cx);

if cfg!(target_os = "macos") {
Expand All @@ -44,16 +48,20 @@ fn init(_: Arc<AppState>, cx: &mut AppContext) -> Result<()> {
Ok(())
}

#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
/// Forces the out of box experience rather than using the token from ENV or appdata
#[arg(long)]
force_oobe: bool,
}

#[tokio::main]
async fn main() {
env_logger::init();

let app_state = Arc::new(AppState {});

App::new().with_assets(Assets).with_http_client(Arc::new(reqwest_client::ReqwestClient::new())).run(move |cx: &mut AppContext| {
AppState::set_global(Arc::downgrade(&app_state), cx);

if let Err(e) = init(app_state.clone(), cx) {
if let Err(e) = init(cx) {
log::error!("{}", e);
return;
}
Expand All @@ -67,17 +75,29 @@ async fn main() {
cx.set_global(theme);
cx.refresh();

let opts = WindowOptions {
window_decorations: Some(WindowDecorations::Client),
window_min_size: Some(size(Pixels(800.0), Pixels(600.0))),
titlebar: Some(TitlebarOptions {
appears_transparent: true,
title: Some(SharedString::new_static("scope")),
..Default::default()
}),
..Default::default()
};

cx.open_window(opts, |cx| cx.new_view(crate::app::App::new)).unwrap();
let mut async_cx = cx.to_async();

let args = Args::parse();

cx.foreground_executor()
.spawn(async move {
if let Some(token) = OobeTokenLogin::get_token(&mut async_cx, args.force_oobe).await {
async_cx.update_global(|global: &mut StateModel, cx| global.provide_token(token, cx)).unwrap();

let opts = WindowOptions {
window_decorations: Some(WindowDecorations::Client),
window_min_size: Some(size(Pixels(800.0), Pixels(600.0))),
titlebar: Some(TitlebarOptions {
appears_transparent: true,
title: Some(SharedString::new_static("scope")),
..Default::default()
}),
..Default::default()
};

async_cx.open_window(opts, |cx| cx.new_view(crate::app::App::new)).unwrap();
}
})
.detach();
});
}
29 changes: 29 additions & 0 deletions src/ui/src/oobe/input.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use components::input::TextInput;
use gpui::{div, rgb, Context, Model, ParentElement, Pixels, Render, Styled, View, VisualContext};

pub struct OobeInput {
title: String,
pub input: View<TextInput>,
}

impl OobeInput {
pub fn create(ctx: &mut gpui::ViewContext<'_, Self>, title: String, secure: bool) -> Self {
let input = ctx.new_view(|cx| {
let mut input = TextInput::new(cx);

if secure {
input.set_masked(true, cx);
}

input
});

OobeInput { title, input }
}
}

impl Render for OobeInput {
fn render(&mut self, _: &mut gpui::ViewContext<Self>) -> impl gpui::IntoElement {
div().flex().flex_col().gap(Pixels(4.)).text_color(rgb(0xA7ACBB)).child(self.title.clone()).child(self.input.clone())
}
}
Loading
Loading