Skip to content
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

Active TabBar is not highlighted initially #18

Open
pombadev opened this issue Feb 18, 2022 · 2 comments
Open

Active TabBar is not highlighted initially #18

pombadev opened this issue Feb 18, 2022 · 2 comments

Comments

@pombadev
Copy link

Hello, I'm following the example verbatim.

use cursive::view::{Nameable, Resizable};
use cursive::views::{Button, LinearLayout, PaddedView, TextArea, TextView};
use cursive_tabs::{Align, TabPanel};
const TAB_0: &str =
"With using the TabPanel you get a TabView and TabBar, preconfigured for you to use!
Simply create it with:
`cursive_tabs::TabPanel::new()`";
const TAB_1: &str = "You then can add views and configure your panel.";
const TAB_2: &str =
"Ofcourse you can also use the provided TabView without the panel, simply create it with:
`cursive_tabs::TabView::new()`";
const TAB_3: &str = "All you have to do is add:
cursive-tabs = \"^0\"
to your Cargo.toml!
";
fn main() {
let mut siv = cursive::default();
let panel = TabPanel::new()
.with_tab(TextView::new(TAB_0).with_name("0"))
.with_tab(TextView::new(TAB_1).with_name("1"))
.with_tab(TextView::new(TAB_2).with_name("2"))
.with_tab(TextView::new(TAB_3).with_name("3"))
.with_tab(PaddedView::lrtb(2, 2, 1, 1, TextArea::new()).with_name("4"))
.with_bar_alignment(Align::End)
.with_active_tab("0")
.unwrap_or_else(|_| {
panic!("Could not set the first tab as active tab! This is probably an issue with the implementation in the lib. Please report!");
});
siv.add_fullscreen_layer(
LinearLayout::vertical()
.child(panel.with_name("Tabs").full_screen())
.child(
LinearLayout::horizontal()
.child(Button::new("Prev", |siv| {
let mut tabs: cursive::views::ViewRef<TabPanel> =
siv.find_name("Tabs").expect("id not found");
tabs.prev();
}))
.child(Button::new("Next", |siv| {
let mut tabs: cursive::views::ViewRef<TabPanel> =
siv.find_name("Tabs").expect("id not found");
tabs.next();
})),
),
);
siv.add_global_callback('q', |siv| siv.quit());
siv.run();
}

The active tab name is not highlighted initially even though active tab is being set.

.with_active_tab("0")

Image to illustrate potential bug and expected behavior.

Screenshot from 2022-02-18 12-51-53 png-mh

Could be the same issue as #4

Package info: cursive-tabs = "0.7.0"

@FOMX
Copy link

FOMX commented Sep 24, 2022

I'm pretty new to rust, but i think this is to do with how the buttons are added to the TabBar of the TabPanel. The TabBar isn't public so i can't see how to set the active button easily, but i have a work around below.

Problem:
Adding a tab using the "add_tab/with_tab" methods always sets the active TabBar Button to the last button.

cursive-tabs/src/bar.rs

Lines 121 to 138 in 52e94cb

impl Bar for TabBar {
fn add_button(&mut self, tx: Sender<String>, key: &str) {
let k = key.to_owned();
let button = Button::new_raw(format!(" {} ", key), move |_| {
debug!("send {}", k);
match tx.send(k.clone()) {
Ok(_) => {}
Err(err) => {
debug!("button could not send key: {:?}", err);
}
}
});
self.children
.push(PositionWrap::new(button, key.to_owned()));
self.cursor = Some(self.children.len() - 1);
self.active = Some(self.children.len() - 1);
self.invalidated = true;
}

Adding a tab using the "add_tab_at" method with an index (pos) sets the active TabBarButton to that index.

cursive-tabs/src/bar.rs

Lines 189 to 205 in 52e94cb

fn add_button_at(&mut self, tx: Sender<String>, key: &str, pos: usize) {
let k = key.to_owned();
let button = Button::new_raw(format!(" {} ", key), move |_| {
debug!("send {}", k);
match tx.send(k.clone()) {
Ok(_) => {}
Err(err) => {
debug!("button could not send key: {:?}", err);
}
}
});
self.cursor = Some(pos);
self.active = Some(pos);
self.children
.insert(pos, PositionWrap::new(button, key.to_owned()));
self.invalidated = true;
}

Workaround:
Add the desired active tab after you've added the other tabs. This will set it's TabBar Button to be active.
Of course you'll need the "add_tab_at" method so the panel will need to be mutable.

    let mut panel = TabPanel::new().with_tab(TextView::new(TAB_1).with_name("1"))
        .with_tab(TextView::new(TAB_2).with_name("2"))
        .with_tab(TextView::new(TAB_3).with_name("3"))
        .with_tab(PaddedView::lrtb(2, 2, 1, 1, TextArea::new()).with_name("4"))
        .with_bar_alignment(Align::End);
    panel.add_tab_at(TextView::new(TAB_0).with_name("0"), 0);
    panel.set_active_tab("0").unwrap_or_else(|_| {
        panic!("Could not set the first tab as active tab! This is probably an issue with the implementation in the lib. Please report!");
    });

image

@shi-yan
Copy link

shi-yan commented Nov 28, 2022

How to focus on a tab panel's internal component, instead of one of the tabs?

For example, I have a text editor, I hope to focus on it, instead of the containing tab panel.

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants