Skip to content

Commit

Permalink
pass Rc browser to UI object
Browse files Browse the repository at this point in the history
  • Loading branch information
d0iasm committed May 1, 2024
1 parent 74e88a3 commit 4669e9d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 104 deletions.
7 changes: 3 additions & 4 deletions main/saba_cui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,11 @@ fn handle_url(url: String) -> Result<HttpResponse, Error> {
}

fn main() {
// initialize the UI object
let ui = Rc::new(RefCell::new(Tui::new()));

// initialize the main browesr struct
let browser = Browser::new();
ui.borrow_mut().set_browser(Rc::downgrade(&browser));

// initialize the UI object
let ui = Rc::new(RefCell::new(Tui::new(browser)));

match ui.borrow_mut().start(handle_url) {
Ok(_) => {}
Expand Down
7 changes: 3 additions & 4 deletions main/saba_wasabi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,11 @@ fn handle_url(url: String) -> Result<HttpResponse, Error> {
}

fn main() -> u64 {
// initialize the UI object
let ui = Rc::new(RefCell::new(WasabiUI::new()));

// initialize the main browesr struct
let browser = Browser::new();
ui.borrow_mut().set_browser(Rc::downgrade(&browser));

// initialize the UI object
let ui = Rc::new(RefCell::new(WasabiUI::new(browser)));

match ui.borrow_mut().start(handle_url) {
Ok(_) => {}
Expand Down
70 changes: 19 additions & 51 deletions ui/cui/src/app.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use alloc::rc::Weak;
use alloc::rc::Rc;
use alloc::string::ToString;
use core::cell::RefCell;
use crossterm::{
Expand Down Expand Up @@ -45,23 +45,20 @@ impl Link {

#[derive(Clone, Debug)]
pub struct Tui {
browser: Weak<RefCell<Browser>>,
browser: Rc<RefCell<Browser>>,
input_url: String,
input_mode: InputMode,
// A user can focus only a link now.
focus: Option<Link>,
// The position that starts rendering a next display item.
//position: (f64, f64),
}

impl Tui {
pub fn new() -> Self {
pub fn new(browser: Rc<RefCell<Browser>>) -> Self {
Self {
browser: Weak::new(),
browser,
input_url: String::new(),
input_mode: InputMode::Normal,
focus: None,
//position: (0.0, 0.0),
}
}

Expand Down Expand Up @@ -92,7 +89,7 @@ impl Tui {
match size() {
Ok((cols, rows)) => {
console_debug(
self.browser.clone(),
Rc::downgrade(&self.browser),
format!("cols rows {:?} {:?}", cols, rows),
);
}
Expand Down Expand Up @@ -126,20 +123,12 @@ impl Tui {
}
}

pub fn set_browser(&mut self, browser: Weak<RefCell<Browser>>) {
self.browser = browser;
}

pub fn browser(&self) -> Weak<RefCell<Browser>> {
pub fn browser(&self) -> Rc<RefCell<Browser>> {
self.browser.clone()
}

fn move_focus_to_up(&mut self) {
let browser = match self.browser().upgrade() {
Some(browser) => browser,
None => return,
};
let display_items = browser.borrow().display_items();
let display_items = self.browser.borrow().display_items();

let mut previous_link_item: Option<Link> = None;
for item in display_items {
Expand Down Expand Up @@ -174,11 +163,7 @@ impl Tui {
}

fn move_focus_to_down(&mut self) {
let browser = match self.browser().upgrade() {
Some(browser) => browser,
None => return,
};
let display_items = browser.borrow().display_items();
let display_items = self.browser.borrow().display_items();

let mut focus_item_found = false;
for item in display_items {
Expand Down Expand Up @@ -218,27 +203,18 @@ impl Tui {
) -> Result<(), Error> {
match handle_url(destination) {
Ok(response) => {
let page = match self.browser().upgrade() {
Some(browser) => {
// clean up Browser struct
{
browser.borrow_mut().clear_display_items();
}
{
browser.borrow_mut().clear_logs();
}

browser.borrow().page()
}
None => {
return Err(Error::Other("associated browser is not found".to_string()))
}
};
// clean up Browser struct
{
let mut b = self.browser.borrow_mut();
b.clear_display_items();
b.clear_logs();
}

let page = self.browser.borrow().page();
page.borrow_mut().receive_response(response);
}
Err(e) => {
console_error(self.browser.clone(), format!("{:?}", e));
console_error(Rc::downgrade(&self.browser), format!("{:?}", e));
return Err(e);
}
}
Expand All @@ -247,11 +223,6 @@ impl Tui {

/*
fn push_key_event(&mut self, key_code: KeyCode) {
let browser = match self.browser().upgrade() {
Some(browser) => browser,
None => return,
};
// https://docs.rs/crossterm/latest/crossterm/event/enum.KeyCode.html
let key = match key_code {
KeyCode::Char(c) => c.to_string(),
Expand Down Expand Up @@ -423,11 +394,7 @@ impl Tui {
}
}

let browser = match self.browser().upgrade() {
Some(browser) => browser,
None => return,
};
let display_items = browser.borrow().display_items();
let display_items = self.browser.borrow().display_items();

/*
let content_area = Layout::default()
Expand Down Expand Up @@ -510,7 +477,8 @@ impl Tui {
.wrap(Wrap { trim: true });
frame.render_widget(contents, chunks[2]);

let logs: Vec<ListItem> = browser
let logs: Vec<ListItem> = self
.browser
.borrow()
.logs()
.iter()
Expand Down
60 changes: 15 additions & 45 deletions ui/wasabi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
extern crate alloc;

use alloc::format;
use alloc::rc::Weak;
use alloc::rc::Rc;
use alloc::string::String;
use alloc::string::ToString;
use alloc::vec;
Expand Down Expand Up @@ -57,17 +57,17 @@ static _CHAR_HEIGHT: i64 = 16;

#[derive(Clone, Debug)]
pub struct WasabiUI {
browser: Weak<RefCell<Browser>>,
browser: Rc<RefCell<Browser>>,
input_url: String,
window: Window,
// The (x, y) position to render a next display item.
position: (i64, i64),
}

impl WasabiUI {
pub fn new() -> Self {
pub fn new(browser: Rc<RefCell<Browser>>) -> Self {
Self {
browser: Weak::new(),
browser,
input_url: String::new(),
window: Window::new(
"SaBA".to_string(),
Expand All @@ -94,11 +94,7 @@ impl WasabiUI {
Ok(())
}

pub fn set_browser(&mut self, browser: Weak<RefCell<Browser>>) {
self.browser = browser;
}

pub fn browser(&self) -> Weak<RefCell<Browser>> {
pub fn browser(&self) -> Rc<RefCell<Browser>> {
self.browser.clone()
}

Expand Down Expand Up @@ -282,23 +278,14 @@ impl WasabiUI {
) -> Result<(), Error> {
match handle_url(destination) {
Ok(response) => {
let page = match self.browser().upgrade() {
Some(browser) => {
// clean up Browser struct
{
browser.borrow_mut().clear_display_items();
}
{
browser.borrow_mut().clear_logs();
}

browser.borrow().page()
}
None => {
return Err(Error::Other("associated browser is not found".to_string()))
}
};
// clean up Browser struct
{
let mut b = self.browser.borrow_mut();
b.clear_display_items();
b.clear_logs();
}

let page = self.browser.borrow().page();
page.borrow_mut().receive_response(response);
}
Err(e) => {
Expand All @@ -309,15 +296,7 @@ impl WasabiUI {
}

fn update_ui(&mut self) -> Result<(), Error> {
let browser = match self.browser().upgrade() {
Some(browser) => browser,
None => {
return Err(Error::Other(
"failed to obtain a browser object".to_string(),
))
}
};
let display_items = browser.borrow().display_items();
let display_items = self.browser.borrow().display_items();

for item in display_items {
match item {
Expand Down Expand Up @@ -391,16 +370,7 @@ impl WasabiUI {
} => {
print!("DisplayItem::Img src: {}\n", src);

match self.browser().upgrade() {
Some(browser) => {
browser.borrow_mut().push_url_for_subresource(src);
}
None => {
return Err(Error::Other(
"failed to obtain a browser object".to_string(),
))
}
};
self.browser.borrow_mut().push_url_for_subresource(src);

let data = include_bytes!("./test.bmp");
let bmp = match Bmp::<Rgb888>::from_slice(data) {
Expand Down Expand Up @@ -433,7 +403,7 @@ impl WasabiUI {
}
}

for log in browser.borrow().logs() {
for log in self.browser.borrow().logs() {
print!("{}\n", log.to_string());
}

Expand Down

0 comments on commit 4669e9d

Please sign in to comment.