Skip to content

Commit

Permalink
Set address to default when creating new tab
Browse files Browse the repository at this point in the history
Closes #3
  • Loading branch information
andrewhickman committed Dec 30, 2021
1 parent 20e8174 commit 66ec8fd
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 12 deletions.
16 changes: 10 additions & 6 deletions src/app/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ impl State {
}
}

pub fn select_or_create_method_tab(&mut self, method: &MethodDescriptor) {
pub fn select_or_create_method_tab(
&mut self,
method: &MethodDescriptor,
options: &ServiceOptions,
) {
if self
.with_selected_method(|_, tab_data| tab_data.method() == method)
.unwrap_or(false)
Expand All @@ -80,7 +84,7 @@ impl State {
}
}

self.create_method_tab(method)
self.create_method_tab(method, options)
}

pub fn select_or_create_options_tab(
Expand Down Expand Up @@ -108,10 +112,10 @@ impl State {
self.create_options_tab(service, options)
}

pub fn create_method_tab(&mut self, method: &MethodDescriptor) {
pub fn create_method_tab(&mut self, method: &MethodDescriptor, options: &ServiceOptions) {
let id = TabId::next();
self.selected = Some(id);
Arc::make_mut(&mut self.tabs).insert(id, TabState::empty_method(method.clone()));
Arc::make_mut(&mut self.tabs).insert(id, TabState::empty_method(method.clone(), options));
}

pub fn create_options_tab(&mut self, service: &ServiceDescriptor, options: &ServiceOptions) {
Expand Down Expand Up @@ -255,8 +259,8 @@ impl State {
}

impl TabState {
fn empty_method(method: prost_reflect::MethodDescriptor) -> TabState {
TabState::Method(MethodTabState::empty(method))
fn empty_method(method: prost_reflect::MethodDescriptor, options: &ServiceOptions) -> TabState {
TabState::Method(MethodTabState::empty(method, options))
}

pub fn new_method(
Expand Down
6 changes: 3 additions & 3 deletions src/app/body/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use druid::{
};

use self::controller::MethodTabController;
use crate::{grpc::MethodKind, json::JsonText, theme};
use crate::{app::sidebar::service::ServiceOptions, grpc::MethodKind, json::JsonText, theme};

#[derive(Debug, Clone, Data, Lens)]
pub struct MethodTabState {
Expand Down Expand Up @@ -52,9 +52,9 @@ pub fn build_body() -> impl Widget<MethodTabState> {
}

impl MethodTabState {
pub fn empty(method: prost_reflect::MethodDescriptor) -> Self {
pub fn empty(method: prost_reflect::MethodDescriptor, options: &ServiceOptions) -> Self {
MethodTabState {
address: address::AddressState::default(),
address: address::AddressState::with_options(options),
stream: stream::State::new(),
request: request::State::empty(method.input()),
method,
Expand Down
11 changes: 10 additions & 1 deletion src/app/body/method/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ use http::Uri;
use crate::{
app::{
body::{RequestState, VALIDATE_URI},
command, theme,
command,
sidebar::service::ServiceOptions,
theme,
},
grpc::MethodKind,
widget::{Empty, FormField, Icon, ValidationState, FINISH_EDIT},
Expand Down Expand Up @@ -195,6 +197,13 @@ impl AddressState {
}
}

pub fn with_options(options: &ServiceOptions) -> AddressState {
match &options.default_address {
Some(addr) => AddressState::new(addr.to_string()),
None => AddressState::default(),
}
}

pub fn is_valid(&self) -> bool {
self.uri.is_valid()
}
Expand Down
8 changes: 6 additions & 2 deletions src/app/delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,18 @@ impl AppDelegate<app::State> for Delegate {
data.body.select_or_create_options_tab(service, options);
Handled::Yes
} else if let Some(method) = cmd.get(command::SELECT_OR_CREATE_METHOD_TAB) {
data.body.select_or_create_method_tab(method);
if let Some(options) = data.sidebar.get_service_options(method.parent_service()) {
data.body.select_or_create_method_tab(method, options);
}
Handled::Yes
} else if let Some(service_index) = cmd.get(command::REMOVE_SERVICE) {
let service = data.sidebar.remove_service(*service_index);
data.body.remove_service(service.service());
Handled::Yes
} else if let Some(method) = cmd.get(command::CREATE_TAB) {
data.body.create_method_tab(method);
if let Some(options) = data.sidebar.get_service_options(method.parent_service()) {
data.body.create_method_tab(method, options);
}
Handled::Yes
} else {
Handled::No
Expand Down
13 changes: 13 additions & 0 deletions src/app/sidebar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,19 @@ impl ServiceListState {
self.services.remove(index)
}

pub fn get_service_options(
&self,
service: &ServiceDescriptor,
) -> Option<&service::ServiceOptions> {
for service_state in self.services.iter() {
if service_state.service() == service {
return Some(service_state.options());
}
}

None
}

pub fn set_service_options(
&mut self,
service: &ServiceDescriptor,
Expand Down

0 comments on commit 66ec8fd

Please sign in to comment.