Skip to content

Commit

Permalink
update code for newer rust and add preliminary settings
Browse files Browse the repository at this point in the history
  • Loading branch information
x86y committed Mar 18, 2024
1 parent 995a672 commit 702a455
Show file tree
Hide file tree
Showing 12 changed files with 160 additions and 82 deletions.
110 changes: 95 additions & 15 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ use iced::font;
use iced::widget::button;
use iced::widget::responsive;
use iced::widget::svg;
use iced::widget::text_input;
use iced::widget::Row;
use iced::widget::Space;
use iced::Font;
use pane_grid::Configuration;
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::collections::VecDeque;
use std::env;
use views::panes::style;
use views::panes::view_controls;
use views::panes::Pane;
Expand Down Expand Up @@ -68,6 +70,15 @@ struct App {
filter: WatchlistFilter,
filter_string: String,
data: AppData,
current_view: ViewState,
api_key: String,
api_secret_key: String,
}

#[derive(PartialEq)]
enum ViewState {
Dashboard,
Settings,
}

#[derive(Default)]
Expand All @@ -82,6 +93,10 @@ struct AppData {

#[derive(Debug, Clone)]
pub enum Message {
SettingsApiKeyChanged(String),
SettingsApiSecretChanged(String),
SetDashboardView,
SetSettingsView,
Split(pane_grid::Axis, pane_grid::Pane),
SplitFocused(pane_grid::Axis),
FocusAdjacent(pane_grid::Direction),
Expand Down Expand Up @@ -168,7 +183,7 @@ impl Application for App {
panes,
panes_created: 1,
focus: None,
filter: WatchlistFilter::Btc,
filter: WatchlistFilter::Favorites,
filter_string: "".to_string(),
watchlist_favorites: [
"BTCUSDT", "ETHUSDT", "LINKUSDT", "UNIUSDT", "ARBUSDT", "SYNUSDT", "OPUSDT",
Expand All @@ -181,6 +196,13 @@ impl Application for App {
new_pair: "BTCUSDT".into(),
pair_submitted: true,
data: Default::default(),
current_view: ViewState::Dashboard,
api_key: env::var_os("DYN_PUB")
.map(|s| s.into_string().unwrap())
.unwrap(),
api_secret_key: env::var_os("DYN_SEC")
.map(|s| s.into_string().unwrap())
.unwrap(),
},
Command::batch(vec![
Command::perform(api::orders_history(), Message::OrdersRecieved),
Expand All @@ -197,6 +219,26 @@ impl Application for App {

fn update(&mut self, message: Message) -> Command<Message> {
match message {
Message::SettingsApiKeyChanged(value) => {
self.api_key = value;
Command::none()
}
Message::SettingsApiSecretChanged(value) => {
self.api_secret_key = value;
Command::none()
}
Message::SetSettingsView => {
if self.current_view == ViewState::Settings {
self.current_view = ViewState::Dashboard;
} else {
self.current_view = ViewState::Settings;
}
Command::none()
}
Message::SetDashboardView => {
self.current_view = ViewState::Dashboard;
Command::none()
}
Message::Split(axis, pane) => {
let result = self
.panes
Expand Down Expand Up @@ -490,13 +532,12 @@ impl Application for App {
let pane_grid = PaneGrid::new(&self.panes, |id, pane, is_maximized| {
let is_focused = focus == Some(id);

let title =
row![text(pane.id.to_string()).style(if is_focused {
PANE_ID_COLOR_FOCUSED
} else {
PANE_ID_COLOR_UNFOCUSED
})]
.spacing(5);
let title = row![text(pane.id.to_string()).style(if is_focused {
PANE_ID_COLOR_FOCUSED
} else {
PANE_ID_COLOR_UNFOCUSED
})]
.spacing(5);

let title_bar = pane_grid::TitleBar::new(title)
.controls(view_controls(id, total_panes, pane.is_pinned, is_maximized))
Expand Down Expand Up @@ -541,9 +582,11 @@ impl Application for App {
.map(|t| {
let price_now = self.data.prices.get(t).unwrap_or(&0.0);
let ticker = t.split("USDT").next().unwrap();
let handle = svg::Handle::from_path(
format!("{}/assets/logos/{}.svg", env!("CARGO_MANIFEST_DIR"), ticker)
);
let handle = svg::Handle::from_path(format!(
"{}/assets/logos/{}.svg",
env!("CARGO_MANIFEST_DIR"),
ticker
));

let svg = svg(handle)
.width(Length::Fixed(16.0))
Expand All @@ -559,8 +602,31 @@ impl Application for App {
button("Settings")
.padding(8)
.style(iced::theme::Button::Text)
.on_press(Message::SetSettingsView)
]
.align_items(iced::Alignment::Center);

let api_key_input = text_input("API Key", &self.api_key)
.secure(true)
.width(Length::Fill)
.on_input(Message::SettingsApiKeyChanged);
let api_secret_key_input = text_input("API Secret Key", &self.api_secret_key)
.secure(true)
.width(Length::Fill)
.on_input(Message::SettingsApiSecretChanged);

let settings = column![
row![text("API Key:").width(Length::Fixed(100.0)), api_key_input].spacing(10),
row![
text("API Secret Key:").width(Length::Fixed(100.0)),
api_secret_key_input,
]
.spacing(10),
]
.spacing(10)
.width(Length::Fill)
.align_items(iced::Alignment::Center);

let message_log: Element<_> = if self.data.prices.is_empty() {
container(text("Loading...").style(Color::from_rgb8(0x88, 0x88, 0x88)))
.width(Length::Fill)
Expand All @@ -569,10 +635,24 @@ impl Application for App {
.center_y()
.into()
} else {
column![container(column![header, pane_grid].spacing(8))
.width(Length::Fill)
.height(Length::Fill)
.padding(10),]
column![container(
column![
header,
if self.current_view == ViewState::Dashboard {
container(pane_grid)
} else {
container(
column![settings]
.width(Length::Fill)
.height(Length::Fill)
)
}
]
.spacing(8)
)
.width(Length::Fill)
.height(Length::Fill)
.padding(10),]
.into()
};

Expand Down
30 changes: 24 additions & 6 deletions src/views/components/better_btn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ impl button::StyleSheet for BetterBtn {
type Style = iced::Theme;
fn active(&self, _: &Self::Style) -> button::Appearance {
button::Appearance {
background: Some(iced::Background::Color(Color::from_rgba(0.3, 0.3, 0.3, 0.3))),
background: Some(iced::Background::Color(Color::from_rgba(
0.3, 0.3, 0.3, 0.3,
))),
text_color: Color::WHITE,
shadow_offset: iced::Vector { x: 1.0, y: 1.0 },
border: iced::Border {
Expand All @@ -20,7 +22,9 @@ impl button::StyleSheet for BetterBtn {

fn pressed(&self, _: &Self::Style) -> button::Appearance {
button::Appearance {
background: Some(iced::Background::Color(Color::from_rgba(0.3, 0.3, 0.3, 0.3))),
background: Some(iced::Background::Color(Color::from_rgba(
0.3, 0.3, 0.3, 0.3,
))),
text_color: Color::WHITE,
shadow_offset: iced::Vector { x: 1.0, y: 1.0 },
border: iced::Border {
Expand All @@ -39,7 +43,12 @@ impl button::StyleSheet for GreenBtn {
type Style = iced::Theme;
fn active(&self, _: &Self::Style) -> button::Appearance {
button::Appearance {
background: Some(iced::Background::Color(Color::from_rgba(0.0, 1.0, 0.0, 1.0))),
background: Some(iced::Background::Color(Color::from_rgba(
50.0 / 255.0,
217.0 / 255.0,
147.0 / 255.0,
1.0,
))),
text_color: Color::WHITE,
shadow_offset: iced::Vector { x: 1.0, y: 1.0 },
border: iced::Border {
Expand All @@ -53,7 +62,9 @@ impl button::StyleSheet for GreenBtn {

fn pressed(&self, _: &Self::Style) -> button::Appearance {
button::Appearance {
background: Some(iced::Background::Color(Color::from_rgba(0.3, 0.3, 0.3, 0.3))),
background: Some(iced::Background::Color(Color::from_rgba(
0.3, 0.3, 0.3, 0.3,
))),
text_color: Color::WHITE,
shadow_offset: iced::Vector { x: 1.0, y: 1.0 },
border: iced::Border {
Expand All @@ -72,7 +83,12 @@ impl button::StyleSheet for RedBtn {
type Style = iced::Theme;
fn active(&self, _: &Self::Style) -> button::Appearance {
button::Appearance {
background: Some(iced::Background::Color(Color::from_rgba(1.0, 0.0, 0.0, 1.0))),
background: Some(iced::Background::Color(Color::from_rgba(
1.0,
112.0 / 255.0,
126.0 / 255.0,
1.0,
))),
text_color: Color::WHITE,
shadow_offset: iced::Vector { x: 1.0, y: 1.0 },
border: iced::Border {
Expand All @@ -86,7 +102,9 @@ impl button::StyleSheet for RedBtn {

fn pressed(&self, _: &Self::Style) -> button::Appearance {
button::Appearance {
background: Some(iced::Background::Color(Color::from_rgba(0.3, 0.3, 0.3, 0.3))),
background: Some(iced::Background::Color(Color::from_rgba(
0.3, 0.3, 0.3, 0.3,
))),
text_color: Color::WHITE,
shadow_offset: iced::Vector { x: 1.0, y: 1.0 },
border: iced::Border {
Expand Down
2 changes: 1 addition & 1 deletion src/views/panes/book.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::BTreeMap;

use iced::{
widget::{column, row, scrollable, Column, Container, Rule},
widget::{column, row, scrollable, Column, Container},
Element, Length,
};

Expand Down
2 changes: 1 addition & 1 deletion src/views/panes/market.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::views::components::better_btn::{BetterBtn, GreenBtn, RedBtn};
use crate::views::components::better_btn::{GreenBtn, RedBtn};
use crate::views::components::input::Inp;
use iced::{
widget::{button, column, container, row, text, text_input, Space},
Expand Down
2 changes: 1 addition & 1 deletion src/views/panes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl From<usize> for PaneType {
impl ToString for PaneType {
fn to_string(&self) -> String {
match self {
PaneType::Prices => "Prices",
PaneType::Prices => "Watchlist",
PaneType::Book => "Book",
PaneType::Trades => "Trades",
PaneType::Market => "Market",
Expand Down
11 changes: 5 additions & 6 deletions src/views/panes/orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::HashMap;

use binance::rest_model::{Order, OrderType};

use crate::{theme::h2c, views::components::scrollbar::ScrollbarStyle, Message};
use crate::{theme::h2c, Message};
use iced::{
widget::{column, container, row, scrollable, text, Column, Space},
Element, Font, Length,
Expand Down Expand Up @@ -41,13 +41,12 @@ pub fn orders_view<'a>(os: &[Order], ps: &'a HashMap<String, f32>) -> Element<'a
tb("Status").width(Length::Fixed(100.0)),
tb("PNL").width(Length::Fixed(100.0))
]
.padding(12)
.padding([0, 12])
.width(Length::Fill);

let rows: Vec<Element<_>> = os
.iter()
.enumerate()
.map(|(i, b)| {
.map(|b| {
let time_t = {
let dt: chrono::DateTime<chrono::Utc> =
chrono::TimeZone::timestamp_opt(&chrono::Utc, (b.time / 1000) as i64, 0)
Expand Down Expand Up @@ -94,14 +93,14 @@ pub fn orders_view<'a>(os: &[Order], ps: &'a HashMap<String, f32>) -> Element<'a
]
.width(Length::Fill),
)
.padding(4)
.padding([2, 4])
.into()
})
.collect();

column![
header,
scrollable(Column::with_children(rows).padding(8)).style(ScrollbarStyle::theme())
scrollable(Column::with_children(rows).padding(8)) //.style(ScrollbarStyle::theme())
]
.into()
}
7 changes: 3 additions & 4 deletions src/views/panes/trades.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use iced::{
Color, Element, Length,
};

use crate::{theme::h2c, views::components::scrollbar::ScrollbarStyle, Message};
use crate::{theme::h2c, Message};

use super::orders::{t, tb};

Expand Down Expand Up @@ -38,9 +38,8 @@ pub fn trades_view(bs: &VecDeque<TradesEvent>) -> Element<'_, Message> {
.width(Length::Fill)
})
.map(Element::from),
))
.style(ScrollbarStyle::theme())
)) // .style(ScrollbarStyle::theme())
]
.padding(12)
.padding([2, 12])
.into()
}
Loading

0 comments on commit 702a455

Please sign in to comment.