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

Question: centering window and light theme #1800

Closed
gko opened this issue May 22, 2021 · 3 comments
Closed

Question: centering window and light theme #1800

gko opened this issue May 22, 2021 · 3 comments

Comments

@gko
Copy link

gko commented May 22, 2021

Hello,

Thank you for druid.

Is this a proper way to center a window?

use druid::widget::Label;
use druid::{AppLauncher, PlatformError, Point, Screen, Size, Widget, WindowDesc};

fn build_ui() -> impl Widget<()> {
    Label::new("")
}

fn main() -> Result<(), PlatformError> {
    let window_size = Size {
        width: 500.0,
        height: 100.0,
    };

    if let Some(primary_display) = Screen::get_monitors()
        .iter()
        .find(|monitor| monitor.is_primary())
    {
        let main_window = WindowDesc::new(build_ui())
            .set_position(Point {
                x: primary_display.virtual_work_rect().center().x - window_size.width / 2.0,
                y: primary_display.virtual_work_rect().center().y - window_size.height / 2.0,
            })
            .window_size(window_size);

        AppLauncher::with_window(main_window)
            .launch(())
            .expect("failed to launch");
    }

    Ok(())
}

And is there any way to set a light theme without setting each color?
By default it is a dark theme:

druid/druid/src/theme.rs

Lines 120 to 185 in 99a8d66

pub(crate) fn add_to_env(env: Env) -> Env {
env.adding(WINDOW_BACKGROUND_COLOR, Color::rgb8(0x29, 0x29, 0x29))
.adding(TEXT_COLOR, Color::rgb8(0xf0, 0xf0, 0xea))
.adding(DISABLED_TEXT_COLOR, Color::rgb8(0xa0, 0xa0, 0x9a))
.adding(PLACEHOLDER_COLOR, Color::rgb8(0x80, 0x80, 0x80))
.adding(PRIMARY_LIGHT, Color::rgb8(0x5c, 0xc4, 0xff))
.adding(PRIMARY_DARK, Color::rgb8(0x00, 0x8d, 0xdd))
.adding(PROGRESS_BAR_RADIUS, 4.)
.adding(BACKGROUND_LIGHT, Color::rgb8(0x3a, 0x3a, 0x3a))
.adding(BACKGROUND_DARK, Color::rgb8(0x31, 0x31, 0x31))
.adding(FOREGROUND_LIGHT, Color::rgb8(0xf9, 0xf9, 0xf9))
.adding(FOREGROUND_DARK, Color::rgb8(0xbf, 0xbf, 0xbf))
.adding(DISABLED_FOREGROUND_LIGHT, Color::rgb8(0x89, 0x89, 0x89))
.adding(DISABLED_FOREGROUND_DARK, Color::rgb8(0x6f, 0x6f, 0x6f))
.adding(BUTTON_DARK, Color::BLACK)
.adding(BUTTON_LIGHT, Color::rgb8(0x21, 0x21, 0x21))
.adding(DISABLED_BUTTON_DARK, Color::grey8(0x28))
.adding(DISABLED_BUTTON_LIGHT, Color::grey8(0x38))
.adding(BUTTON_BORDER_RADIUS, 4.)
.adding(BUTTON_BORDER_WIDTH, 2.)
.adding(BORDER_DARK, Color::rgb8(0x3a, 0x3a, 0x3a))
.adding(BORDER_LIGHT, Color::rgb8(0xa1, 0xa1, 0xa1))
.adding(
SELECTED_TEXT_BACKGROUND_COLOR,
Color::rgb8(0x43, 0x70, 0xA8),
)
.adding(SELECTED_TEXT_INACTIVE_BACKGROUND_COLOR, Color::grey8(0x74))
.adding(SELECTION_TEXT_COLOR, Color::rgb8(0x00, 0x00, 0x00))
.adding(CURSOR_COLOR, Color::WHITE)
.adding(TEXT_SIZE_NORMAL, 15.0)
.adding(TEXT_SIZE_LARGE, 24.0)
.adding(BASIC_WIDGET_HEIGHT, 18.0)
.adding(WIDE_WIDGET_WIDTH, 100.)
.adding(BORDERED_WIDGET_HEIGHT, 24.0)
.adding(TEXTBOX_BORDER_RADIUS, 2.)
.adding(TEXTBOX_BORDER_WIDTH, 1.)
.adding(TEXTBOX_INSETS, Insets::new(4.0, 4.0, 4.0, 4.0))
.adding(SCROLLBAR_COLOR, Color::rgb8(0xff, 0xff, 0xff))
.adding(SCROLLBAR_BORDER_COLOR, Color::rgb8(0x77, 0x77, 0x77))
.adding(SCROLLBAR_MAX_OPACITY, 0.7)
.adding(SCROLLBAR_FADE_DELAY, 1500u64)
.adding(SCROLLBAR_WIDTH, 8.)
.adding(SCROLLBAR_PAD, 2.)
.adding(SCROLLBAR_MIN_SIZE, 45.)
.adding(SCROLLBAR_RADIUS, 5.)
.adding(SCROLLBAR_EDGE_WIDTH, 1.)
.adding(WIDGET_PADDING_VERTICAL, 10.0)
.adding(WIDGET_PADDING_HORIZONTAL, 8.0)
.adding(WIDGET_CONTROL_COMPONENT_PADDING, 4.0)
.adding(
UI_FONT,
FontDescriptor::new(FontFamily::SYSTEM_UI).with_size(15.0),
)
.adding(
UI_FONT_BOLD,
FontDescriptor::new(FontFamily::SYSTEM_UI)
.with_weight(FontWeight::BOLD)
.with_size(15.0),
)
.adding(
UI_FONT_ITALIC,
FontDescriptor::new(FontFamily::SYSTEM_UI)
.with_style(FontStyle::Italic)
.with_size(15.0),
)
}

@xarvic
Copy link
Collaborator

xarvic commented May 27, 2021

Is this a proper way to center a window?

I could not think of a better one right now.

And is there any way to set a light theme without setting each color?

Not at the moment, but theming is being worked on.

@SecondFlight
Copy link
Collaborator

It's worth noting that Wayland doesn't allow top-level windows to be positioned. You can still position subwindows relative to their parent windows in Wayland. See here for more info: https://gitlab.freedesktop.org/wayland/wayland/-/issues/183

@santokalayil
Copy link

For positioning window to the center of screen I have used this code. Here I kept the window size to half of the screen size and placed in the 1/4 position of the screen

    let main_window = WindowDesc::new(build_root_widget).title(WINDOW_TITLE).window_size(
        (druid::Screen::get_display_rect().size() / 2.0)
        .to_vec2()
    )
    .set_position((druid::Screen::get_display_rect().size() / 4.0).to_vec2().to_point(),)
    .show_titlebar(false);

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

4 participants