Workspaces: Move fSwitchOnWheel setting to view

... and re-enable it. Off by default. The view controls just this
one setting while the window controls the rest. However they both
share a common settings file on disk. This means that a change in
one view's wheel setting will affect all others when relaunched,
but won't affect already running instances, this is intentional.

WorkspacesSettings::_Open() method has been moved to a static
function OpenSettingsFile() and is used both by WorkspacesSettings
and WorkspacesView classes.

The switch on wheel setting was disabled for view-only (replicant)
Workspaces instances in hrev50796, this seeks to restore it.

That commit says:
"A cleaner solution would be to read the settings once if there is
no WorkspacesWindow, and use the setting from there instead."

Instead of that, we read the setting from the file on disk and
write it to disk too bypassing the window entirely.

Used local pointer style.
Some style fixes mixed in.
This commit is contained in:
John Scipione
2017-11-02 15:21:58 -07:00
parent 1769813948
commit cf6569ab6a
+120 -77
View File
@@ -60,6 +60,33 @@ static const uint32 kMsgToggleSwitchOnWheel = 'tgWh';
extern "C" _EXPORT BView* instantiate_deskbar_item(); extern "C" _EXPORT BView* instantiate_deskbar_item();
static status_t
OpenSettingsFile(BFile& file, int mode)
{
BPath path;
status_t status = find_directory(B_USER_SETTINGS_DIRECTORY, &path);
if (status != B_OK)
status = find_directory(B_SYSTEM_SETTINGS_DIRECTORY, &path);
if (status != B_OK)
return status;
status = path.Append(kSettingsFile);
if (status != B_OK)
return status;
status = file.SetTo(path.Path(), mode);
if (mode == B_READ_ONLY && status == B_ENTRY_NOT_FOUND) {
if (find_directory(B_SYSTEM_SETTINGS_DIRECTORY, &path) == B_OK
&& path.Append(kSettingsFile) == B_OK) {
status = file.SetTo(path.Path(), mode);
}
}
return status;
}
class WorkspacesSettings { class WorkspacesSettings {
public: public:
WorkspacesSettings(); WorkspacesSettings();
@@ -72,7 +99,6 @@ class WorkspacesSettings {
bool AlwaysOnTop() const { return fAlwaysOnTop; } bool AlwaysOnTop() const { return fAlwaysOnTop; }
bool HasTitle() const { return fHasTitle; } bool HasTitle() const { return fHasTitle; }
bool HasBorder() const { return fHasBorder; } bool HasBorder() const { return fHasBorder; }
bool SwitchOnWheel() const { return fSwitchOnWheel; }
bool SettingsLoaded() const { return fLoaded; } bool SettingsLoaded() const { return fLoaded; }
void UpdateFramesForScreen(BRect screenFrame); void UpdateFramesForScreen(BRect screenFrame);
@@ -83,18 +109,14 @@ class WorkspacesSettings {
void SetAlwaysOnTop(bool enable) { fAlwaysOnTop = enable; } void SetAlwaysOnTop(bool enable) { fAlwaysOnTop = enable; }
void SetHasTitle(bool enable) { fHasTitle = enable; } void SetHasTitle(bool enable) { fHasTitle = enable; }
void SetHasBorder(bool enable) { fHasBorder = enable; } void SetHasBorder(bool enable) { fHasBorder = enable; }
void SetSwitchOnWheel(bool enable) { fSwitchOnWheel = enable; }
private: private:
status_t _Open(BFile& file, int mode);
BRect fWindowFrame; BRect fWindowFrame;
BRect fScreenFrame; BRect fScreenFrame;
bool fAutoRaising; bool fAutoRaising;
bool fAlwaysOnTop; bool fAlwaysOnTop;
bool fHasTitle; bool fHasTitle;
bool fHasBorder; bool fHasBorder;
bool fSwitchOnWheel;
bool fLoaded; bool fLoaded;
}; };
@@ -116,6 +138,9 @@ class WorkspacesView : public BView {
const BMessage* dragMessage); const BMessage* dragMessage);
virtual void MouseDown(BPoint where); virtual void MouseDown(BPoint where);
bool SwitchOnWheel() const { return fSwitchOnWheel; }
void SetSwitchOnWheel(bool enable);
private: private:
void _AboutRequested(); void _AboutRequested();
@@ -123,8 +148,12 @@ class WorkspacesView : public BView {
void _ExcludeFromParentClipping(); void _ExcludeFromParentClipping();
void _CleanupParentClipping(); void _CleanupParentClipping();
void _LoadSettings();
void _SaveSettings();
BView* fParentWhichDrawsOnChildren; BView* fParentWhichDrawsOnChildren;
BRect fCurrentFrame; BRect fCurrentFrame;
bool fSwitchOnWheel;
}; };
class WorkspacesWindow : public BWindow { class WorkspacesWindow : public BWindow {
@@ -142,8 +171,6 @@ class WorkspacesWindow : public BWindow {
void SetAutoRaise(bool enable); void SetAutoRaise(bool enable);
bool IsAutoRaising() const { return fSettings->AutoRaising(); } bool IsAutoRaising() const { return fSettings->AutoRaising(); }
void SetSwitchOnWheel(bool enable);
bool SwitchOnWheel() const { return fSwitchOnWheel; }
float GetTabHeight() { return fSettings->HasTitle() ? fTabHeight : 0; } float GetTabHeight() { return fSettings->HasTitle() ? fTabHeight : 0; }
float GetBorderWidth() { return fBorderWidth; } float GetBorderWidth() { return fBorderWidth; }
@@ -151,7 +178,7 @@ class WorkspacesWindow : public BWindow {
private: private:
WorkspacesSettings *fSettings; WorkspacesSettings *fSettings;
bool fSwitchOnWheel; WorkspacesView *fWorkspacesView;
float fTabHeight; float fTabHeight;
float fBorderWidth; float fBorderWidth;
}; };
@@ -172,13 +199,15 @@ class WorkspacesApp : public BApplication {
}; };
// #pragma mark - WorkspacesSettings
WorkspacesSettings::WorkspacesSettings() WorkspacesSettings::WorkspacesSettings()
: :
fAutoRaising(false), fAutoRaising(false),
fAlwaysOnTop(false), fAlwaysOnTop(false),
fHasTitle(true), fHasTitle(true),
fHasBorder(true), fHasBorder(true),
fSwitchOnWheel(false),
fLoaded(false) fLoaded(false)
{ {
UpdateScreenFrame(); UpdateScreenFrame();
@@ -186,22 +215,17 @@ WorkspacesSettings::WorkspacesSettings()
BScreen screen; BScreen screen;
BFile file; BFile file;
if (_Open(file, B_READ_ONLY) == B_OK) { if (OpenSettingsFile(file, B_READ_ONLY) == B_OK) {
BMessage settings; BMessage settings;
if (settings.Unflatten(&file) == B_OK) { if (settings.Unflatten(&file) == B_OK) {
if (settings.FindRect("window", &fWindowFrame) == B_OK fLoaded = settings.FindRect("window", &fWindowFrame) == B_OK
&& settings.FindRect("screen", &fScreenFrame) == B_OK) && settings.FindRect("screen", &fScreenFrame) == B_OK;
fLoaded = true;
settings.FindBool("auto-raise", &fAutoRaising); settings.FindBool("auto-raise", &fAutoRaising);
settings.FindBool("always on top", &fAlwaysOnTop); settings.FindBool("always on top", &fAlwaysOnTop);
if (settings.FindBool("has title", &fHasTitle) != B_OK) if (settings.FindBool("has title", &fHasTitle) != B_OK)
fHasTitle = true; fHasTitle = true;
if (settings.FindBool("has border", &fHasBorder) != B_OK) if (settings.FindBool("has border", &fHasBorder) != B_OK)
fHasBorder = true; fHasBorder = true;
if (settings.FindBool("switch on wheel", &fSwitchOnWheel) != B_OK)
fSwitchOnWheel = false;
} }
} else { } else {
// try reading BeOS compatible settings // try reading BeOS compatible settings
@@ -235,45 +259,30 @@ WorkspacesSettings::WorkspacesSettings()
WorkspacesSettings::~WorkspacesSettings() WorkspacesSettings::~WorkspacesSettings()
{ {
// write settings file
BFile file; BFile file;
if (_Open(file, B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE) != B_OK)
// read switch on wheel setting from file
bool switchOnWheel = false;
if (OpenSettingsFile(file, B_READ_ONLY) == B_OK) {
BMessage settings;
if (settings.Unflatten(&file) == B_OK)
settings.FindBool("switch on wheel", &switchOnWheel);
}
// write settings file
if (OpenSettingsFile(file, B_WRITE_ONLY | B_CREATE_FILE) != B_OK)
return; return;
BMessage settings('wksp'); BMessage settings('wksp');
if (settings.AddRect("window", fWindowFrame) == B_OK if (settings.AddRect("window", fWindowFrame) == B_OK
&& settings.AddRect("screen", fScreenFrame) == B_OK && settings.AddRect("screen", fScreenFrame) == B_OK
&& settings.AddBool("auto-raise", fAutoRaising) == B_OK && settings.AddBool("auto-raise", fAutoRaising) == B_OK
&& settings.AddBool("always on top", fAlwaysOnTop) == B_OK && settings.AddBool("always on top", fAlwaysOnTop) == B_OK
&& settings.AddBool("has title", fHasTitle) == B_OK && settings.AddBool("has title", fHasTitle) == B_OK
&& settings.AddBool("has border", fHasBorder) == B_OK && settings.AddBool("has border", fHasBorder) == B_OK
&& settings.AddBool("switch on wheel", fSwitchOnWheel) == B_OK) && settings.AddBool("switch on wheel", switchOnWheel) == B_OK) {
settings.Flatten(&file); settings.Flatten(&file);
} }
status_t
WorkspacesSettings::_Open(BFile& file, int mode)
{
BPath path;
status_t status = find_directory(B_USER_SETTINGS_DIRECTORY, &path);
if (status != B_OK)
status = find_directory(B_SYSTEM_SETTINGS_DIRECTORY, &path);
if (status != B_OK)
return status;
path.Append(kSettingsFile);
status = file.SetTo(path.Path(), mode);
if (mode == B_READ_ONLY && status == B_ENTRY_NOT_FOUND) {
if (find_directory(B_SYSTEM_SETTINGS_DIRECTORY, &path) == B_OK) {
path.Append(kSettingsFile);
status = file.SetTo(path.Path(), mode);
}
}
return status;
} }
@@ -315,7 +324,7 @@ WorkspacesSettings::SetWindowFrame(BRect frame)
} }
// #pragma mark - // #pragma mark - WorkspacesView
WorkspacesView::WorkspacesView(BRect frame, bool showDragger = true) WorkspacesView::WorkspacesView(BRect frame, bool showDragger = true)
@@ -323,8 +332,11 @@ WorkspacesView::WorkspacesView(BRect frame, bool showDragger=true)
BView(frame, kDeskbarItemName, B_FOLLOW_ALL, BView(frame, kDeskbarItemName, B_FOLLOW_ALL,
kWorkspacesViewFlag | B_FRAME_EVENTS), kWorkspacesViewFlag | B_FRAME_EVENTS),
fParentWhichDrawsOnChildren(NULL), fParentWhichDrawsOnChildren(NULL),
fCurrentFrame(frame) fCurrentFrame(frame),
fSwitchOnWheel(false)
{ {
_LoadSettings();
if (showDragger) { if (showDragger) {
frame.OffsetTo(B_ORIGIN); frame.OffsetTo(B_ORIGIN);
frame.top = frame.bottom - 7; frame.top = frame.bottom - 7;
@@ -340,8 +352,11 @@ WorkspacesView::WorkspacesView(BMessage* archive)
: :
BView(archive), BView(archive),
fParentWhichDrawsOnChildren(NULL), fParentWhichDrawsOnChildren(NULL),
fCurrentFrame(Frame()) fCurrentFrame(Frame()),
fSwitchOnWheel(false)
{ {
_LoadSettings();
// Just in case we are instantiated from an older archive... // Just in case we are instantiated from an older archive...
SetFlags(Flags() | B_FRAME_EVENTS); SetFlags(Flags() | B_FRAME_EVENTS);
// Make sure the auto-raise feature didn't leave any artifacts - this is // Make sure the auto-raise feature didn't leave any artifacts - this is
@@ -353,6 +368,7 @@ WorkspacesView::WorkspacesView(BMessage* archive)
WorkspacesView::~WorkspacesView() WorkspacesView::~WorkspacesView()
{ {
_SaveSettings();
} }
@@ -479,6 +495,32 @@ WorkspacesView::_CleanupParentClipping()
} }
void
WorkspacesView::_LoadSettings()
{
BFile file;
if (OpenSettingsFile(file, B_READ_ONLY) == B_OK) {
BMessage settings;
if (settings.Unflatten(&file) == B_OK)
settings.FindBool("switch on wheel", &fSwitchOnWheel);
}
}
void
WorkspacesView::_SaveSettings()
{
BFile file;
if (OpenSettingsFile(file, B_READ_WRITE | B_CREATE_FILE) != B_OK)
return;
BMessage settings('wksp');
settings.Unflatten(&file);
if (settings.AddBool("switch on wheel", fSwitchOnWheel) == B_OK)
settings.Flatten(&file);
}
void void
WorkspacesView::MessageReceived(BMessage* message) WorkspacesView::MessageReceived(BMessage* message)
{ {
@@ -489,9 +531,7 @@ WorkspacesView::MessageReceived(BMessage* message)
case B_MOUSE_WHEEL_CHANGED: case B_MOUSE_WHEEL_CHANGED:
{ {
WorkspacesWindow* window if (!fSwitchOnWheel)
= dynamic_cast<WorkspacesWindow*>(Window());
if (window == NULL || !window->SwitchOnWheel())
break; break;
float deltaY = message->FindFloat("be:wheel_delta_y"); float deltaY = message->FindFloat("be:wheel_delta_y");
@@ -516,6 +556,12 @@ WorkspacesView::MessageReceived(BMessage* message)
break; break;
} }
case kMsgToggleSwitchOnWheel:
{
fSwitchOnWheel = !fSwitchOnWheel;
break;
}
default: default:
BView::MessageReceived(message); BView::MessageReceived(message);
break; break;
@@ -578,16 +624,16 @@ WorkspacesView::MouseDown(BPoint where)
B_UTF8_ELLIPSIS), new BMessage(kMsgChangeCount)); B_UTF8_ELLIPSIS), new BMessage(kMsgChangeCount));
menu->AddItem(changeItem); menu->AddItem(changeItem);
BMenuItem* switchItem = new BMenuItem(B_TRANSLATE("Switch on mouse wheel"),
new BMessage(kMsgToggleSwitchOnWheel));
menu->AddItem(switchItem);
switchItem->SetMarked(fSwitchOnWheel);
WorkspacesWindow *window = dynamic_cast<WorkspacesWindow*>(Window()); WorkspacesWindow *window = dynamic_cast<WorkspacesWindow*>(Window());
if (window != NULL) { if (window != NULL) {
// inside Workspaces app // inside Workspaces app
BMenuItem* item; BMenuItem* item;
menu->AddSeparatorItem();
menu->AddItem(item = new BMenuItem(B_TRANSLATE("Switch on mouse wheel"),
new BMessage(kMsgToggleSwitchOnWheel)));
item->SetMarked(window->SwitchOnWheel());
menu->AddSeparatorItem(); menu->AddSeparatorItem();
menu->AddItem(item = new BMenuItem(B_TRANSLATE("Show window tab"), menu->AddItem(item = new BMenuItem(B_TRANSLATE("Show window tab"),
new BMessage(kMsgToggleTitle))); new BMessage(kMsgToggleTitle)));
@@ -596,8 +642,9 @@ WorkspacesView::MouseDown(BPoint where)
menu->AddItem(item = new BMenuItem(B_TRANSLATE("Show window border"), menu->AddItem(item = new BMenuItem(B_TRANSLATE("Show window border"),
new BMessage(kMsgToggleBorder))); new BMessage(kMsgToggleBorder)));
if (window->Look() == B_TITLED_WINDOW_LOOK if (window->Look() == B_TITLED_WINDOW_LOOK
|| window->Look() == B_MODAL_WINDOW_LOOK) || window->Look() == B_MODAL_WINDOW_LOOK) {
item->SetMarked(true); item->SetMarked(true);
}
menu->AddSeparatorItem(); menu->AddSeparatorItem();
menu->AddItem(item = new BMenuItem(B_TRANSLATE("Always on top"), menu->AddItem(item = new BMenuItem(B_TRANSLATE("Always on top"),
@@ -642,12 +689,24 @@ WorkspacesView::MouseDown(BPoint where)
} }
changeItem->SetTarget(this); changeItem->SetTarget(this);
switchItem->SetTarget(this);
ConvertToScreen(&where); ConvertToScreen(&where);
menu->Go(where, true, true, true); menu->Go(where, true, true, true);
} }
// #pragma mark - void
WorkspacesView::SetSwitchOnWheel(bool enable)
{
if (enable == fSwitchOnWheel)
return;
fSwitchOnWheel = enable;
}
// #pragma mark - WorkspacesWindow
WorkspacesWindow::WorkspacesWindow(WorkspacesSettings *settings) WorkspacesWindow::WorkspacesWindow(WorkspacesSettings *settings)
@@ -657,7 +716,7 @@ WorkspacesWindow::WorkspacesWindow(WorkspacesSettings *settings)
B_AVOID_FRONT | B_WILL_ACCEPT_FIRST_CLICK | B_CLOSE_ON_ESCAPE, B_AVOID_FRONT | B_WILL_ACCEPT_FIRST_CLICK | B_CLOSE_ON_ESCAPE,
B_ALL_WORKSPACES), B_ALL_WORKSPACES),
fSettings(settings), fSettings(settings),
fSwitchOnWheel(false) fWorkspacesView(NULL)
{ {
// Turn window decor on to grab decor widths. // Turn window decor on to grab decor widths.
BMessage windowSettings; BMessage windowSettings;
@@ -720,14 +779,13 @@ WorkspacesWindow::WorkspacesWindow(WorkspacesSettings *settings)
else if (!fSettings->HasTitle()) else if (!fSettings->HasTitle())
SetLook(B_MODAL_WINDOW_LOOK); SetLook(B_MODAL_WINDOW_LOOK);
AddChild(new WorkspacesView(Bounds())); fWorkspacesView = new WorkspacesView(Bounds());
AddChild(fWorkspacesView);
if (fSettings->AlwaysOnTop()) if (fSettings->AlwaysOnTop())
SetFeel(B_FLOATING_ALL_WINDOW_FEEL); SetFeel(B_FLOATING_ALL_WINDOW_FEEL);
else else
SetAutoRaise(fSettings->AutoRaising()); SetAutoRaise(fSettings->AutoRaising());
SetSwitchOnWheel(fSettings->SwitchOnWheel());
} }
@@ -904,10 +962,6 @@ WorkspacesWindow::MessageReceived(BMessage *message)
break; break;
} }
case kMsgToggleSwitchOnWheel:
SetSwitchOnWheel(!SwitchOnWheel());
break;
default: default:
BWindow::MessageReceived(message); BWindow::MessageReceived(message);
break; break;
@@ -935,18 +989,7 @@ WorkspacesWindow::SetAutoRaise(bool enable)
} }
void // #pragma mark - WorkspacesApp
WorkspacesWindow::SetSwitchOnWheel(bool enable)
{
if (enable == fSwitchOnWheel)
return;
fSwitchOnWheel = enable;
fSettings->SetSwitchOnWheel(enable);
}
// #pragma mark -
WorkspacesApp::WorkspacesApp() WorkspacesApp::WorkspacesApp()