Skip to content

Commit

Permalink
tab list and switch
Browse files Browse the repository at this point in the history
  • Loading branch information
duckfromdiscord committed Oct 28, 2024
1 parent 251c707 commit c878d2f
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 1 deletion.
49 changes: 49 additions & 0 deletions src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use uuid::Uuid;
pub enum RequestType {
ListThemes,
SetTheme(String),
ListTabs(TabQuery),
SwapTab(u16),
}

impl RequestType {
Expand All @@ -21,6 +23,8 @@ impl RequestType {
uuid,
command: "list_themes".to_string(),
theme_id: None,
query: None,
index: None,
};
Ok(serde_json::to_string(&req)?.as_bytes().to_vec())
}
Expand All @@ -29,6 +33,28 @@ impl RequestType {
uuid,
command: "set_theme".to_string(),
theme_id: Some(theme_id.to_string()),
query: None,
index: None,
};
Ok(serde_json::to_string(&req)?.as_bytes().to_vec())
}
RequestType::ListTabs(query) => {
let req = ExtensionRequest {
uuid,
command: "list_tabs".to_string(),
theme_id: None,
query: Some(query.clone()),
index: None,
};
Ok(serde_json::to_string(&req)?.as_bytes().to_vec())
}
RequestType::SwapTab(tab) => {
let req = ExtensionRequest {
uuid,
command: "select_tab".to_string(),
theme_id: None,
query: None,
index: Some(*tab),
};
Ok(serde_json::to_string(&req)?.as_bytes().to_vec())
}
Expand Down Expand Up @@ -84,12 +110,35 @@ async fn set_theme(info: web::Query<SetThemeInfo>, data: web::Data<AppState>) ->
HttpResponse::Ok().finish()
}

#[get("/get_tabs")]
async fn get_tabs(info: web::Query<TabQuery>, data: web::Data<AppState>) -> impl Responder {
let req_id = uuid::Uuid::new_v4();
match push_request_and_wait(req_id, data, RequestType::ListTabs(info.0)).await {
Some(extresp) => HttpResponse::Ok().body(serde_json::to_string(&extresp.tabs).unwrap()),
None => HttpResponse::InternalServerError().finish(),
}
}

#[derive(Deserialize)]
struct SetTab {
id: u16,
}

#[get("/swap_tab")]
async fn swap_tab(info: web::Query<SetTab>, data: web::Data<AppState>) -> impl Responder {
let req_id = uuid::Uuid::new_v4();
push_request(req_id, data, RequestType::SwapTab(info.id));
HttpResponse::Ok().finish()
}

pub fn http_server(state: AppState) -> std::io::Result<()> {
let state = web::Data::new(state);
let server = HttpServer::new(move || {
App::new()
.service(get_themes)
.service(set_theme)
.service(get_tabs)
.service(swap_tab)
.app_data(state.clone())
})
.bind(("127.0.0.1", 8080))?;
Expand Down
47 changes: 47 additions & 0 deletions src/json.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,41 @@
use serde_derive::*;

#[derive(Debug, PartialEq, Deserialize, Serialize, Clone, Default)]
pub struct TabQuery {
#[serde(skip_serializing_if = "Option::is_none")]
pub active: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub attention: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub audible: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none", rename = "currentWindow")]
pub current_window: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub index: Option<u16>,
#[serde(skip_serializing_if = "Option::is_none")]
pub muted: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub pinned: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub title: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none", rename = "windowId")]
pub window_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none", rename = "windowType")]
pub window_type: Option<String>,
}

#[derive(Debug, PartialEq, Deserialize, Serialize, Clone)]
pub struct ExtensionRequest {
pub uuid: String,
pub command: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub theme_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub query: Option<TabQuery>,
#[serde(skip_serializing_if = "Option::is_none")]
pub index: Option<u16>,
}

#[derive(Debug, PartialEq, Deserialize, Serialize, Clone)]
Expand All @@ -13,9 +44,25 @@ pub struct Theme {
pub id: String,
}

#[derive(Debug, PartialEq, Deserialize, Serialize, Clone)]
pub struct Tab {
pub active: bool,
pub id: u16,
pub index: u16,
pub pinned: bool,
pub title: String,
pub url: String,
#[serde(rename = "windowId")]
pub window_id: u16,
}

#[derive(Debug, PartialEq, Deserialize, Serialize, Clone)]
pub struct ExtensionResponse {
pub uuid: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub themes: Option<Vec<Theme>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub success: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tabs: Option<Vec<Tab>>,
}
1 change: 0 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let host_path = proj_dirs.data_dir().join("host.json");
let script_path = proj_dirs.data_dir().join("nmhhost.bat");


natemess::install::nmh_files_setup(
&batch_contents,
host_path.clone(),
Expand Down

0 comments on commit c878d2f

Please sign in to comment.