Ticket #8194: Workspaces-Constants-Removed.diff

File Workspaces-Constants-Removed.diff, 8.2 KB (added by devine, 12 years ago)

Initial work to get rid of hardcoded decorator constants.

  • src/apps/workspaces/Workspaces.cpp

    diff --git a/src/apps/workspaces/Workspaces.cpp b/src/apps/workspaces/Workspaces.cpp
    index e7b99f9..c6eb7a9 100644
    a b  
    2727#include <Screen.h>
    2828#include <TextView.h>
    2929#include <Window.h>
    30 
    3130#include <ctype.h>
    3231#include <stdio.h>
    3332#include <stdlib.h>
    static const uint32 kMsgToggleAutoRaise = 'tgAR';  
    5554static const uint32 kMsgToggleAlwaysOnTop = 'tgAT';
    5655static const uint32 kMsgToggleLiveInDeskbar = 'tgDb';
    5756
    58 static const float kScreenBorderOffset = 10.0;
    5957
    6058extern "C" _EXPORT BView* instantiate_deskbar_item();
    6159
    class WorkspacesSettings {  
    7169        bool AlwaysOnTop() const { return fAlwaysOnTop; }
    7270        bool HasTitle() const { return fHasTitle; }
    7371        bool HasBorder() const { return fHasBorder; }
     72        bool SettingsLoaded() const { return fLoaded; }
    7473
    7574        void UpdateFramesForScreen(BRect screenFrame);
    7675        void UpdateScreenFrame();
    class WorkspacesSettings {  
    9089        bool    fAlwaysOnTop;
    9190        bool    fHasTitle;
    9291        bool    fHasBorder;
     92        bool    fLoaded;
    9393};
    9494
    9595class WorkspacesView : public BView {
    class WorkspacesWindow : public BWindow {  
    136136
    137137        void SetAutoRaise(bool enable);
    138138        bool IsAutoRaising() const { return fAutoRaising; }
     139        float GetTabHeight() { return fTabHeight; }
     140        float GetBorderWidth() { return fBorderWidth; }
     141        float GetScreenBorderOffset() { return 2.0 * fBorderWidth; }
    139142
    140143    private:
    141144        WorkspacesSettings *fSettings;
    142145        bool    fAutoRaising;
     146        float   fTabHeight;
     147        float   fBorderWidth;
    143148};
    144149
    145150class WorkspacesApp : public BApplication {
    WorkspacesSettings::WorkspacesSettings()  
    166171    fHasBorder(true)
    167172{
    168173    UpdateScreenFrame();
     174   
     175    fLoaded = false;
    169176
    170     bool loaded = false;
    171177    BScreen screen;
    172178
    173179    BFile file;
    WorkspacesSettings::WorkspacesSettings()  
    176182        if (settings.Unflatten(&file) == B_OK) {
    177183            if (settings.FindRect("window", &fWindowFrame) == B_OK
    178184                && settings.FindRect("screen", &fScreenFrame) == B_OK)
    179                 loaded = true;
     185                fLoaded = true;
    180186
    181187            settings.FindBool("auto-raise", &fAutoRaising);
    182188            settings.FindBool("always on top", &fAlwaysOnTop);
    WorkspacesSettings::WorkspacesSettings()  
    202208                else
    203209                    fScreenFrame = screen.Frame();
    204210
    205                 loaded = true;
     211                fLoaded = true;
    206212            }
     213        } else {
     214            fprintf(stderr, "Opening settings file failed.\n");
    207215        }
    208216    }
    209217
    210     if (loaded) {
     218    if (fLoaded) {
    211219        // if the current screen frame is different from the one
    212220        // just loaded, we need to alter the window frame accordingly
    213221        if (fScreenFrame != screen.Frame())
    214222            UpdateFramesForScreen(screen.Frame());
    215223    }
    216 
    217     if (!loaded
    218         || !(screen.Frame().right + 5 >= fWindowFrame.right
    219             && screen.Frame().bottom + 5 >= fWindowFrame.bottom
    220             && screen.Frame().left - 5 <= fWindowFrame.left
    221             && screen.Frame().top - 5 <= fWindowFrame.top)) {
    222         // set to some usable defaults
    223         float screenWidth = screen.Frame().Width();
    224         float screenHeight = screen.Frame().Height();
    225         float aspectRatio = screenWidth / screenHeight;
    226 
    227         uint32 columns, rows;
    228         BPrivate::get_workspaces_layout(&columns, &rows);
    229 
    230         // default size of ~1/10 of screen width
    231         float workspaceWidth = screenWidth / 10;
    232         float workspaceHeight = workspaceWidth / aspectRatio;
    233 
    234         float width = floor(workspaceWidth * columns);
    235         float height = floor(workspaceHeight * rows);
    236 
    237         float tabHeight = 20;
    238             // TODO: find tabHeight without being a window
    239 
    240         // shrink to fit more
    241         while (width + 2 * kScreenBorderOffset > screenWidth
    242             || height + 2 * kScreenBorderOffset + tabHeight > screenHeight) {
    243             width = floor(0.95 * width);
    244             height = floor(0.95 * height);
    245         }
    246 
    247         fWindowFrame = fScreenFrame;
    248         fWindowFrame.OffsetBy(-kScreenBorderOffset, -kScreenBorderOffset);
    249         fWindowFrame.left = fWindowFrame.right - width;
    250         fWindowFrame.top = fWindowFrame.bottom - height;
    251     }
    252224}
    253225
    254226
    WorkspacesSettings::~WorkspacesSettings()  
    260232        return;
    261233
    262234    BMessage settings('wksp');
    263 
     235       
    264236    if (settings.AddRect("window", fWindowFrame) == B_OK
    265237        && settings.AddRect("screen", fScreenFrame) == B_OK
    266238        && settings.AddBool("auto-raise", fAutoRaising) == B_OK
    WorkspacesSettings::~WorkspacesSettings()  
    268240        && settings.AddBool("has title", fHasTitle) == B_OK
    269241        && settings.AddBool("has border", fHasBorder) == B_OK)
    270242        settings.Flatten(&file);
     243    else
     244        fprintf(stderr,"An error occured while saving settings.\n");
    271245}
    272246
    273247
    WorkspacesSettings::UpdateFramesForScreen(BRect newScreenFrame)  
    313287        fWindowFrame.OffsetTo(fWindowFrame.left,
    314288            newScreenFrame.bottom - (fScreenFrame.bottom - fWindowFrame.top));
    315289    }
    316 
    317290    fScreenFrame = newScreenFrame;
    318291}
    319292
    WorkspacesSettings::SetWindowFrame(BRect frame)  
    339312WorkspacesView::WorkspacesView(BRect frame, bool showDragger=true)
    340313    :
    341314    BView(frame, kDeskbarItemName, B_FOLLOW_ALL,
    342         kWorkspacesViewFlag | B_FRAME_EVENTS),
    343     fParentWhichDrawsOnChildren(NULL),
     315        kWorkspacesViewFlag | B_FRAME_EVENTS),    // kWorkspacesViewFlag is picked up by the app_server.
     316    fParentWhichDrawsOnChildren(NULL),            // ...That's how the desktop frames are done!
    344317    fCurrentFrame(frame)
    345318{
    346319    if(showDragger) {
    WorkspacesWindow::WorkspacesWindow(WorkspacesSettings *settings)  
    645618    fSettings(settings),
    646619    fAutoRaising(false)
    647620{
    648     AddChild(new WorkspacesView(Bounds()));
     621   
     622    // Turn window decor on to grab decor widths.
     623    BMessage windowSettings;
     624    SetLook(B_TITLED_WINDOW_LOOK);
     625    if (GetDecoratorSettings(&windowSettings) == B_OK){
     626        BRect tabFrame = windowSettings.FindRect("tab frame");
     627        float borderWidth = windowSettings.FindFloat("border width");
     628        fTabHeight = tabFrame.Height();
     629        fBorderWidth = borderWidth;
     630       
     631        /* Note: Below is based on old code from WorkspacesSettings::WorkspacesSettings().
     632                 Decorator dimensions can be dynamically fetched instead of hardcoded here. */
     633       
     634        BScreen screen;
     635       
     636        float screenWidth = screen.Frame().Width();
     637        float screenHeight = screen.Frame().Height();
     638        float aspectRatio = screenWidth / screenHeight;
     639
     640        uint32 columns, rows;
     641        BPrivate::get_workspaces_layout(&columns, &rows);
     642
     643        // default size of ~1/10 of screen width
     644        float workspaceWidth = screenWidth / 10;
     645        float workspaceHeight = workspaceWidth / aspectRatio;
     646
     647        float width = floor(workspaceWidth * columns);
     648        float height = floor(workspaceHeight * rows);
     649       
     650        // If you have too many workspaces to fit on the screen, shrink until they fit.
     651        while (width + 2 * borderWidth > screenWidth
     652            || height + 2 * borderWidth + GetTabHeight() > screenHeight) {
     653            width = floor(0.95 * width);
     654            height = floor(0.95 * height);
     655        }
     656       
     657        BRect frame = fSettings->ScreenFrame();
     658        frame.OffsetBy(-2.0 * borderWidth, -2.0 * borderWidth);
     659        frame.left = frame.right - width;
     660        frame.top = frame.bottom - height;
     661        ResizeTo(frame.Width(), frame.Height());
     662        if (!fSettings->SettingsLoaded()) {
     663            // Put it in bottom corner by default.
     664            MoveTo(screenWidth - frame.Width() - borderWidth,
     665                    screenHeight - frame.Height() - borderWidth);
     666        }
     667        fSettings->SetWindowFrame(frame);
     668    }
    649669
    650670    if (!fSettings->HasBorder())
    651671        SetLook(B_NO_BORDER_WINDOW_LOOK);
    WorkspacesWindow::WorkspacesWindow(WorkspacesSettings *settings)  
    656676        SetFeel(B_FLOATING_ALL_WINDOW_FEEL);
    657677    else
    658678        SetAutoRaise(fSettings->AutoRaising());
     679       
     680        AddChild(new WorkspacesView(Bounds()));
    659681}
    660682
    661683
    WorkspacesWindow::Zoom(BPoint origin, float width, float height)  
    724746    width = floor(workspaceWidth * columns);
    725747    height = floor(workspaceHeight * rows);
    726748
    727     float tabHeight = Frame().top - DecoratorFrame().top;
    728749
    729     while (width + 2 * kScreenBorderOffset > screenWidth
    730         || height + 2 * kScreenBorderOffset + tabHeight > screenHeight) {
     750    while (width + 2 * GetScreenBorderOffset() > screenWidth
     751        || height + 2 * GetScreenBorderOffset() + GetTabHeight() > screenHeight) {
    731752        width = floor(0.95 * width);
    732753        height = floor(0.95 * height);
    733754    }
    WorkspacesWindow::Zoom(BPoint origin, float width, float height)  
    735756    ResizeTo(width, height);
    736757
    737758    origin = screen.Frame().RightBottom();
    738     origin.x -= kScreenBorderOffset + width;
    739     origin.y -= kScreenBorderOffset + height;
     759    origin.x -= GetScreenBorderOffset() + width;
     760    origin.y -= GetScreenBorderOffset() + height;
    740761
    741762    MoveTo(origin);
    742763}