Complete make over of DecorSettingsView inner workings

* Cleaned up header (why declare some methods protected?!)
 * Refactoring by applying basic "write only once" rule
 * DecorInfoUtility is now cached as member
 * This also fixes multiple instances of it being leaked.
 * Return code of SetDecorator() is now checked, so it should be hard
   to go out of sync with actually visible Decorator.
 * fSavedDecor and fCurrentDecor were pointers to the memory of
   temporary BStrings, this never worked!
 * IsDefaultable() always returned true, despite it being pretty easy
   to implement properly.
 * Revert() didn't do anything, probably because fSavedDecor was never
   initialized anywhere.
 * The UI was not updated in all situations to decorator changes.

Alexander, please have a close look at these changes.
This commit is contained in:
Stephan Aßmus
2012-02-26 10:46:42 +01:00
parent c9ce04c45e
commit f4183b0913
2 changed files with 68 additions and 84 deletions
@@ -1,9 +1,10 @@
/* /*
* Copyright 2010-2011 Haiku, Inc. All rights reserved. * Copyright 2010-2012 Haiku, Inc. All rights reserved.
* Distributed under the terms of the MIT license. * Distributed under the terms of the MIT license.
* *
* Authors: * Authors:
* Alexander von Gluck, [email protected] * Alexander von Gluck, [email protected]
* Stephan Aßmus <[email protected]>
*/ */
@@ -14,6 +15,7 @@
#include <Alert.h> #include <Alert.h>
#include <Box.h> #include <Box.h>
#include <Button.h>
#include <Catalog.h> #include <Catalog.h>
#include <GridLayoutBuilder.h> #include <GridLayoutBuilder.h>
#include <GroupLayoutBuilder.h> #include <GroupLayoutBuilder.h>
@@ -23,7 +25,6 @@
#include <PopUpMenu.h> #include <PopUpMenu.h>
#include <Slider.h> #include <Slider.h>
#include <SpaceLayoutItem.h> #include <SpaceLayoutItem.h>
#include <String.h>
#include <TextView.h> #include <TextView.h>
#include "APRWindow.h" #include "APRWindow.h"
@@ -92,39 +93,15 @@ DecorSettingsView::MessageReceived(BMessage *msg)
case kMsgSetDecor: case kMsgSetDecor:
{ {
BString newDecor; BString newDecor;
if (msg->FindString("decor", &newDecor) != B_OK) if (msg->FindString("decor", &newDecor) == B_OK)
break; _SetDecor(newDecor);
DecorInfoUtility* decorUtility
= new(std::nothrow) DecorInfoUtility();
if (decorUtility == NULL)
return;
DecorInfo* decor = decorUtility->FindDecorator(newDecor);
if (decor == NULL)
return;
fSavedDecor = fCurrentDecor;
fCurrentDecor = (char*)decor->Name().String();
decorUtility->SetDecorator(decor);
Window()->PostMessage(kMsgUpdate);
break; break;
} }
case kMsgDecorInfo: case kMsgDecorInfo:
{ {
DecorInfoUtility* decorUtility DecorInfo* decor = fDecorUtility.FindDecorator(fCurrentDecor);
= new(std::nothrow) DecorInfoUtility();
if (decorUtility == NULL)
return;
BString decoratorName(fCurrentDecor);
DecorInfo* decor = decorUtility->FindDecorator(decoratorName);
if (decor == NULL) if (decor == NULL)
return; break;
BString authorsText(decor->Authors().String()); BString authorsText(decor->Authors().String());
authorsText.ReplaceAll(", ", "\n "); authorsText.ReplaceAll(", ", "\n ");
@@ -146,11 +123,12 @@ DecorSettingsView::MessageReceived(BMessage *msg)
infoAlert->SetShortcut(0, B_ESCAPE); infoAlert->SetShortcut(0, B_ESCAPE);
infoAlert->Go(); infoAlert->Go();
Window()->PostMessage(kMsgUpdate);
break; break;
} }
default: default:
BView::MessageReceived(msg); BView::MessageReceived(msg);
break;
} }
} }
@@ -159,18 +137,11 @@ void
DecorSettingsView::_BuildDecorMenu() DecorSettingsView::_BuildDecorMenu()
{ {
fDecorMenu = new BPopUpMenu(B_TRANSLATE("Choose Decorator")); fDecorMenu = new BPopUpMenu(B_TRANSLATE("Choose Decorator"));
DecorInfo* decorator = NULL;
// collect the current system decor settings // collect the current system decor settings
DecorInfoUtility* decorUtility = new(std::nothrow) DecorInfoUtility(); int32 count = fDecorUtility.CountDecorators();
if (decorUtility == NULL) {
return;
}
int32 count = decorUtility->CountDecorators();
for (int32 i = 0; i < count; ++i) { for (int32 i = 0; i < count; ++i) {
decorator = decorUtility->DecoratorAt(i); DecorInfo* decorator = fDecorUtility.DecoratorAt(i);
if (decorator == NULL) { if (decorator == NULL) {
fprintf(stderr, "Decorator : error NULL entry @ %li / %li\n", fprintf(stderr, "Decorator : error NULL entry @ %li / %li\n",
i, count); i, count);
@@ -186,18 +157,41 @@ DecorSettingsView::_BuildDecorMenu()
fDecorMenu->AddItem(item); fDecorMenu->AddItem(item);
} }
fCurrentDecor = (char*)decorUtility->CurrentDecorator()->Name().String();
delete decorUtility;
_SetCurrentDecor(); _AdoptToCurrentDecor();
} }
void void
DecorSettingsView::_SetCurrentDecor() DecorSettingsView::_SetDecor(const BString& name)
{ {
BMenuItem *item = fDecorMenu->FindItem(fCurrentDecor); _SetDecor(fDecorUtility.FindDecorator(name));
BString currDecor = fCurrentDecor; }
void
DecorSettingsView::_SetDecor(DecorInfo* decorInfo)
{
if (fDecorUtility.SetDecorator(decorInfo) == B_OK) {
_AdoptToCurrentDecor();
Window()->PostMessage(kMsgUpdate);
}
}
void
DecorSettingsView::_AdoptToCurrentDecor()
{
fCurrentDecor = fDecorUtility.CurrentDecorator()->Name();
if (fSavedDecor.Length() == 0)
fSavedDecor = fCurrentDecor;
_AdoptInterfaceToCurrentDecor();
}
void
DecorSettingsView::_AdoptInterfaceToCurrentDecor()
{
BMenuItem* item = fDecorMenu->FindItem(fCurrentDecor);
if (item != NULL) if (item != NULL)
item->SetMarked(true); item->SetMarked(true);
} }
@@ -206,23 +200,14 @@ DecorSettingsView::_SetCurrentDecor()
void void
DecorSettingsView::SetDefaults() DecorSettingsView::SetDefaults()
{ {
DecorInfoUtility* decorUtility _SetDecor(fDecorUtility.DefaultDecorator());
= new(std::nothrow) DecorInfoUtility();
if (decorUtility == NULL)
return;
DecorInfo* defaultDecorator(decorUtility->DefaultDecorator());
decorUtility->SetDecorator(defaultDecorator);
_BuildDecorMenu();
delete decorUtility;
} }
bool bool
DecorSettingsView::IsDefaultable() DecorSettingsView::IsDefaultable()
{ {
return true; return fCurrentDecor != fDecorUtility.DefaultDecorator()->Name();
} }
@@ -236,7 +221,5 @@ DecorSettingsView::IsRevertable()
void void
DecorSettingsView::Revert() DecorSettingsView::Revert()
{ {
if (!IsRevertable()) _SetDecor(fSavedDecor);
return;
} }
+15 -14
View File
@@ -1,23 +1,21 @@
/* /*
* Copyright 2010-2011 Haiku, Inc. All rights reserved. * Copyright 2010-2012 Haiku, Inc. All rights reserved.
* Distributed under the terms of the MIT license. * Distributed under the terms of the MIT license.
* *
* Authors: * Authors:
* Alexander von Gluck, [email protected] * Alexander von Gluck, [email protected]
* Stephan Aßmus <[email protected]>
*/ */
#ifndef DECOR_SETTINGS_VIEW_H #ifndef DECOR_SETTINGS_VIEW_H
#define DECOR_SETTINGS_VIEW_H #define DECOR_SETTINGS_VIEW_H
#include <Button.h> #include <DecorInfo.h>
#include <GroupView.h> #include <String.h>
#include <InterfaceDefs.h>
#include <View.h> #include <View.h>
#include <DecorInfo.h>
class BButton;
class BBox;
class BMenuField; class BMenuField;
class BPopUpMenu; class BPopUpMenu;
@@ -36,19 +34,22 @@ public:
bool IsRevertable(); bool IsRevertable();
private: private:
void _SetDecor(const BString& name);
void _SetDecor(BPrivate::DecorInfo* decorInfo);
void _BuildDecorMenu(); void _BuildDecorMenu();
void _SetCurrentDecor(); void _AdoptToCurrentDecor();
void _AdoptInterfaceToCurrentDecor();
private:
DecorInfoUtility fDecorUtility;
BButton* fDecorInfoButton; BButton* fDecorInfoButton;
protected:
float fDivider;
BMenuField* fDecorMenuField; BMenuField* fDecorMenuField;
BPopUpMenu* fDecorMenu; BPopUpMenu* fDecorMenu;
char* fSavedDecor; BString fSavedDecor;
char* fCurrentDecor; BString fCurrentDecor;
}; };
#endif // DECOR_SETTINGS_VIEW_H #endif // DECOR_SETTINGS_VIEW_H