PackageInstaller: Refactoring and cleanup

* Extraced common base class BlockingWindow from PackageTextViewer and
   PackageImagerViewer.
 * Use layouted version of PackageTextViewer.
 * Center ReadMe on screen.
 * Apply margins around the text view.
This commit is contained in:
Stephan Aßmus
2014-02-11 23:19:11 +01:00
parent 3421284044
commit 6568820634
7 changed files with 191 additions and 248 deletions
@@ -0,0 +1,92 @@
/*
* Copyright (c) 2007-2014, Haiku, Inc.
* Distributed under the terms of the MIT license.
*
* Author:
* Łukasz 'Sil2100' Zemczak <[email protected]>
* Stephan Aßmus <[email protected]>
*/
#include "PackageImageViewer.h"
BlockingWindow::BlockingWindow(BRect frame, const char* title, uint32 flags)
:
BWindow(frame, title, B_MODAL_WINDOW,
B_NOT_ZOOMABLE | B_NOT_RESIZABLE | B_NOT_CLOSABLE | flags),
fSemaphore(-1),
fReturnValue(0)
{
}
BlockingWindow::~BlockingWindow()
{
}
bool
BlockingWindow::QuitRequested()
{
ReleaseSem(0);
return true;
}
int32
BlockingWindow::Go()
{
int32 returnValue = 0;
// Since this class can be thought of as a modified BAlert window, no use
// to reinvent a well fledged wheel. This concept has been borrowed from
// the current BAlert implementation
fSemaphore = create_sem(0, "PackageInstaller BlockingWindow");
if (fSemaphore < B_OK) {
Quit();
return returnValue;
}
thread_id callingThread = find_thread(NULL);
BWindow* window = dynamic_cast<BWindow*>(BLooper::LooperForThread(
callingThread));
Show();
if (window != NULL) {
// Make sure calling window thread, which is blocked here, is updating
// the window from time to time.
status_t ret;
for (;;) {
do {
ret = acquire_sem_etc(fSemaphore, 1, B_RELATIVE_TIMEOUT, 50000);
} while (ret == B_INTERRUPTED);
if (ret == B_BAD_SEM_ID)
break;
window->UpdateIfNeeded();
}
} else {
// Since there are no spinlocks, wait until the semaphore is free
while (acquire_sem(fSemaphore) == B_INTERRUPTED) {
}
}
returnValue = fReturnValue;
if (Lock())
Quit();
return returnValue;
}
void
BlockingWindow::ReleaseSem(int32 returnValue)
{
if (fSemaphore >= B_OK) {
delete_sem(fSemaphore);
fSemaphore = -1;
fReturnValue = returnValue;
}
}
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2014, Stephan Aßmus <[email protected]>
* All rights reserved. Distributed under the terms of the MIT license.
*/
#ifndef BLOCKING_WINDOW_H
#define BLOCKING_WINDOW_H
#include <Window.h>
class BlockingWindow : public BWindow {
public:
BlockingWindow(BRect frame,
const char* title, uint32 flags = 0);
virtual ~BlockingWindow();
virtual bool QuitRequested();
virtual int32 Go();
protected:
void ReleaseSem(int32 returnValue);
private:
sem_id fSemaphore;
int32 fReturnValue;
};
#endif // BLOCKING_WINDOW_H
+1
View File
@@ -10,6 +10,7 @@ Includes [ FGristFiles PackageItem.cpp ]
Application PackageInstaller : Application PackageInstaller :
main.cpp main.cpp
BlockingWindow.cpp
PackageWindow.cpp PackageWindow.cpp
PackageView.cpp PackageView.cpp
PackageInfo.cpp PackageInfo.cpp
@@ -96,8 +96,7 @@ ImageView::MouseUp(BPoint point)
PackageImageViewer::PackageImageViewer(BPositionIO* imageIO) PackageImageViewer::PackageImageViewer(BPositionIO* imageIO)
: :
BWindow(BRect(100, 100, 100, 100), "", B_MODAL_WINDOW, BlockingWindow(BRect(100, 100, 100, 100), "")
B_NOT_ZOOMABLE | B_NOT_RESIZABLE | B_NOT_CLOSABLE)
{ {
fBackground = new ImageView(imageIO); fBackground = new ImageView(imageIO);
AddChild(fBackground); AddChild(fBackground);
@@ -110,60 +109,3 @@ PackageImageViewer::PackageImageViewer(BPositionIO* imageIO)
(frame.Height() - Bounds().Height()) / 2.0f); (frame.Height() - Bounds().Height()) / 2.0f);
} }
PackageImageViewer::~PackageImageViewer()
{
}
bool
PackageImageViewer::QuitRequested()
{
if (fSemaphore >= B_OK) {
delete_sem(fSemaphore);
fSemaphore = -1;
}
return true;
}
void
PackageImageViewer::Go()
{
// Since this class can be thought of as a modified BAlert window, no use
// to reinvent a well fledged wheel. This concept has been borrowed from
// the current BAlert implementation
fSemaphore = create_sem(0, "ImageViewer");
if (fSemaphore < B_OK) {
Quit();
return;
}
thread_id callingThread = find_thread(NULL);
BWindow* window = dynamic_cast<BWindow*>(BLooper::LooperForThread(
callingThread));
Show();
if (window != NULL) {
// Make sure calling window thread, which is blocked here, is updating
// the window from time to time.
status_t ret;
for (;;) {
do {
ret = acquire_sem_etc(fSemaphore, 1, B_RELATIVE_TIMEOUT, 50000);
} while (ret == B_INTERRUPTED);
if (ret == B_BAD_SEM_ID)
break;
window->UpdateIfNeeded();
}
} else {
// Since there are no spinlocks, wait until the semaphore is free
while (acquire_sem(fSemaphore) == B_INTERRUPTED) {
}
}
if (Lock())
Quit();
}
@@ -8,11 +8,11 @@
#ifndef PACKAGE_IMAGE_VIEWER_H #ifndef PACKAGE_IMAGE_VIEWER_H
#define PACKAGE_IMAGE_VIEWER_H #define PACKAGE_IMAGE_VIEWER_H
#include <Window.h>
#include <View.h> #include <View.h>
#include <Bitmap.h> #include <Bitmap.h>
#include <DataIO.h> #include <DataIO.h>
#include "BlockingWindow.h"
class ImageView : public BView { class ImageView : public BView {
@@ -29,19 +29,12 @@ private:
}; };
class PackageImageViewer : public BWindow { class PackageImageViewer : public BlockingWindow {
public: public:
PackageImageViewer(BPositionIO* image); PackageImageViewer(BPositionIO* image);
virtual ~PackageImageViewer();
virtual bool QuitRequested();
void Go();
private: private:
ImageView* fBackground; ImageView* fBackground;
sem_id fSemaphore;
}; };
+55 -164
View File
@@ -1,9 +1,10 @@
/* /*
* Copyright (c) 2007-2010, Haiku, Inc. * Copyright (c) 2007-2014, Haiku, Inc.
* Distributed under the terms of the MIT license. * Distributed under the terms of the MIT license.
* *
* Author: * Author:
* Łukasz 'Sil2100' Zemczak <[email protected]> * Łukasz 'Sil2100' Zemczak <[email protected]>
* Stephan Aßmus <[email protected]>
*/ */
@@ -14,8 +15,7 @@
#include <Locale.h> #include <Locale.h>
#include <ScrollView.h> #include <ScrollView.h>
#include <GroupLayout.h> #include <LayoutBuilder.h>
#include <GroupLayoutBuilder.h>
enum { enum {
@@ -29,78 +29,30 @@ enum {
PackageTextViewer::PackageTextViewer(const char *text, bool disclaimer) PackageTextViewer::PackageTextViewer(const char *text, bool disclaimer)
: :
BWindow(BRect(125, 125, 675, 475), B_TRANSLATE("Disclaimer"), BlockingWindow(BRect(125, 125, 675, 475), B_TRANSLATE("Disclaimer"),
B_MODAL_WINDOW, B_NOT_ZOOMABLE | B_NOT_RESIZABLE | B_NOT_CLOSABLE), B_AUTO_UPDATE_SIZE_LIMITS)
fValue(0)
{ {
_InitView(text, disclaimer); _InitView(text, disclaimer);
} CenterOnScreen();
PackageTextViewer::~PackageTextViewer()
{
}
int32
PackageTextViewer::Go()
{
// Since this class can be thought of as a modified BAlert window, no use
// to reinvent a well fledged wheel. This concept has been borrowed from
// the current BAlert implementation
fSemaphore = create_sem(0, "TextViewer");
if (fSemaphore < B_OK) {
Quit();
return B_ERROR;
}
BWindow *parent =
dynamic_cast<BWindow *>(BLooper::LooperForThread(find_thread(NULL)));
Show();
if (parent) {
status_t ret;
for (;;) {
do {
ret = acquire_sem_etc(fSemaphore, 1, B_RELATIVE_TIMEOUT, 50000);
} while (ret == B_INTERRUPTED);
if (ret == B_BAD_SEM_ID)
break;
parent->UpdateIfNeeded();
}
}
else {
// Since there are no spinlocks, wait until the semaphore is free
while (acquire_sem(fSemaphore) == B_INTERRUPTED) {
}
}
int32 value = fValue;
if (Lock())
Quit();
return value;
} }
void void
PackageTextViewer::MessageReceived(BMessage *msg) PackageTextViewer::MessageReceived(BMessage* message)
{ {
if (msg->what == P_MSG_ACCEPT) { switch (message->what) {
if (fSemaphore >= B_OK) { case P_MSG_ACCEPT:
fValue = 1; ReleaseSem(1);
delete_sem(fSemaphore); break;
fSemaphore = -1;
} case P_MSG_DECLINE:
} else if (msg->what == P_MSG_DECLINE) { ReleaseSem(0);
if (fSemaphore >= B_OK) { break;
fValue = 0;
delete_sem(fSemaphore); default:
fSemaphore = -1; BWindow::MessageReceived(message);
} break;
} else }
BWindow::MessageReceived(msg);
} }
@@ -108,111 +60,50 @@ PackageTextViewer::MessageReceived(BMessage *msg)
void void
PackageTextViewer::_InitView(const char *text, bool disclaimer) PackageTextViewer::_InitView(const char* text, bool disclaimer)
{ {
fBackground = new BView(Bounds(), "background_view", 0, 0); BTextView* textView = new BTextView("text_view");
fBackground->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); textView->MakeEditable(false);
textView->MakeSelectable(true);
float margin = ceilf(be_plain_font->Size());
textView->SetInsets(margin, margin, margin, margin);
BScrollView* scrollView = new BScrollView("scroll_view", textView, 0, false,
true);
BButton* defaultButton;
BRect bounds;
BRect rect = Bounds();
if (disclaimer) { if (disclaimer) {
BButton *button = new BButton(BRect(0, 0, 1, 1), "accept", defaultButton = new BButton("accept", B_TRANSLATE("Accept"),
B_TRANSLATE("Accept"), new BMessage(P_MSG_ACCEPT)); new BMessage(P_MSG_ACCEPT));
button->ResizeToPreferred();
bounds = button->Bounds(); BButton* decline = new BButton("decline", B_TRANSLATE("Decline"),
rect.top = rect.bottom - bounds.bottom - 5.0f; new BMessage(P_MSG_DECLINE));
rect.left = rect.right - bounds.right - 5.0f;
rect.bottom = bounds.bottom;
rect.right = bounds.right;
button->MoveTo(rect.LeftTop());
button->MakeDefault(true);
fBackground->AddChild(button);
button = new BButton(BRect(0, 0, 1, 1), "decline", BLayoutBuilder::Group<>(this, B_VERTICAL)
B_TRANSLATE("Decline"), new BMessage(P_MSG_DECLINE)); .Add(scrollView)
button->ResizeToPreferred(); .AddGroup(B_HORIZONTAL)
.AddGlue()
bounds = button->Bounds(); .Add(defaultButton)
rect.left -= bounds.right + 7.0f; .Add(decline)
button->MoveTo(rect.LeftTop()); .End()
fBackground->AddChild(button); .SetInsets(B_USE_WINDOW_INSETS)
;
} else { } else {
BButton *button = new BButton(BRect(0, 0, 1, 1), "accept", defaultButton = new BButton("accept", B_TRANSLATE("Continue"),
B_TRANSLATE("Continue"), new BMessage(P_MSG_ACCEPT)); new BMessage(P_MSG_ACCEPT));
button->ResizeToPreferred();
bounds = button->Bounds(); BLayoutBuilder::Group<>(this, B_VERTICAL)
rect.top = rect.bottom - bounds.bottom - 5.0f; .Add(scrollView)
rect.left = rect.right - bounds.right - 5.0f; .AddGroup(B_HORIZONTAL)
rect.bottom = bounds.bottom; .AddGlue()
rect.right = bounds.right; .Add(defaultButton)
button->MoveTo(rect.LeftTop()); .End()
button->MakeDefault(true); .SetInsets(B_USE_WINDOW_INSETS)
fBackground->AddChild(button); ;
} }
bounds = Bounds().InsetBySelf(5.0f, 5.0f); defaultButton->MakeDefault(true);
bounds.bottom = rect.top - 6.0f;
bounds.right -= B_V_SCROLL_BAR_WIDTH;
fText = new BTextView(bounds, "text_view", BRect(0, 0, bounds.Width(), textView->SetText(text);
bounds.Height()), B_FOLLOW_NONE, B_WILL_DRAW);
fText->MakeEditable(false);
fText->MakeSelectable(true);
fText->SetText(text);
BScrollView *scroll = new BScrollView("scroll_view", fText,
B_FOLLOW_LEFT | B_FOLLOW_TOP, 0, false, true);
fBackground->AddChild(scroll);
AddChild(fBackground);
} }
/*void
PackageTextViewer::_InitView(const char *text, bool disclaimer)
{
SetLayout(new BGroupLayout(B_HORIZONTAL));
fText = new BTextView(BRect(0, 0, 1, 1), "text_view", BRect(0, 0, 1, 1),
B_FOLLOW_NONE, B_WILL_DRAW | B_SUPPORTS_LAYOUT);
fText->MakeEditable(false);
fText->MakeSelectable(true);
BScrollView *scroll = new BScrollView("scroll_view", fText,
B_FOLLOW_LEFT | B_FOLLOW_TOP, 0, false, true);
if (disclaimer) {
BButton *accept = new BButton("accept", B_TRANSLATE("Accept"),
new BMessage(P_MSG_ACCEPT));
BButton *decline = new BButton("decline", B_TRANSLATE("Decline"),
new BMessage(P_MSG_DECLINE));
fBackground = BGroupLayoutBuilder(B_VERTICAL)
.Add(scroll)
.AddGroup(B_HORIZONTAL, 5.0f)
.AddGlue()
.Add(accept)
.Add(decline)
.End();
}
else {
BButton *button = new BButton("accept", B_TRANSLATE("Continue"),
new BMessage(P_MSG_ACCEPT));
fBackground = BGroupLayoutBuilder(B_VERTICAL)
.Add(scroll)
.AddGroup(B_HORIZONTAL, 5.0f)
.AddGlue()
.Add(button)
.End();
}
AddChild(fBackground);
fBackground->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
fText->SetText(text);
}*/
+8 -16
View File
@@ -8,28 +8,20 @@
#ifndef PACKAGETEXTVIEWER_H #ifndef PACKAGETEXTVIEWER_H
#define PACKAGETEXTVIEWER_H #define PACKAGETEXTVIEWER_H
#include <Window.h>
#include <View.h> #include <View.h>
#include <TextView.h> #include <TextView.h>
#include "BlockingWindow.h"
class PackageTextViewer : public BWindow { class PackageTextViewer : public BlockingWindow {
public: public:
PackageTextViewer(const char *text, bool disclaimer = false); PackageTextViewer(const char* text,
~PackageTextViewer(); bool disclaimer = false);
int32 Go(); virtual void MessageReceived(BMessage* message);
void MessageReceived(BMessage *msg);
private: private:
void _InitView(const char *text, bool disclaimer); void _InitView(const char *text, bool disclaimer);
BView *fBackground;
BTextView *fText;
sem_id fSemaphore;
int32 fValue;
}; };