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 :
main.cpp
BlockingWindow.cpp
PackageWindow.cpp
PackageView.cpp
PackageInfo.cpp
@@ -96,8 +96,7 @@ ImageView::MouseUp(BPoint point)
PackageImageViewer::PackageImageViewer(BPositionIO* imageIO)
:
BWindow(BRect(100, 100, 100, 100), "", B_MODAL_WINDOW,
B_NOT_ZOOMABLE | B_NOT_RESIZABLE | B_NOT_CLOSABLE)
BlockingWindow(BRect(100, 100, 100, 100), "")
{
fBackground = new ImageView(imageIO);
AddChild(fBackground);
@@ -110,60 +109,3 @@ PackageImageViewer::PackageImageViewer(BPositionIO* imageIO)
(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
#define PACKAGE_IMAGE_VIEWER_H
#include <Window.h>
#include <View.h>
#include <Bitmap.h>
#include <DataIO.h>
#include "BlockingWindow.h"
class ImageView : public BView {
@@ -29,19 +29,12 @@ private:
};
class PackageImageViewer : public BWindow {
class PackageImageViewer : public BlockingWindow {
public:
PackageImageViewer(BPositionIO* image);
virtual ~PackageImageViewer();
virtual bool QuitRequested();
void Go();
private:
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.
*
* Author:
* Łukasz 'Sil2100' Zemczak <[email protected]>
* Stephan Aßmus <[email protected]>
*/
@@ -14,8 +15,7 @@
#include <Locale.h>
#include <ScrollView.h>
#include <GroupLayout.h>
#include <GroupLayoutBuilder.h>
#include <LayoutBuilder.h>
enum {
@@ -29,78 +29,30 @@ enum {
PackageTextViewer::PackageTextViewer(const char *text, bool disclaimer)
:
BWindow(BRect(125, 125, 675, 475), B_TRANSLATE("Disclaimer"),
B_MODAL_WINDOW, B_NOT_ZOOMABLE | B_NOT_RESIZABLE | B_NOT_CLOSABLE),
fValue(0)
BlockingWindow(BRect(125, 125, 675, 475), B_TRANSLATE("Disclaimer"),
B_AUTO_UPDATE_SIZE_LIMITS)
{
_InitView(text, disclaimer);
}
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;
CenterOnScreen();
}
void
PackageTextViewer::MessageReceived(BMessage *msg)
PackageTextViewer::MessageReceived(BMessage* message)
{
if (msg->what == P_MSG_ACCEPT) {
if (fSemaphore >= B_OK) {
fValue = 1;
delete_sem(fSemaphore);
fSemaphore = -1;
}
} else if (msg->what == P_MSG_DECLINE) {
if (fSemaphore >= B_OK) {
fValue = 0;
delete_sem(fSemaphore);
fSemaphore = -1;
}
} else
BWindow::MessageReceived(msg);
switch (message->what) {
case P_MSG_ACCEPT:
ReleaseSem(1);
break;
case P_MSG_DECLINE:
ReleaseSem(0);
break;
default:
BWindow::MessageReceived(message);
break;
}
}
@@ -108,111 +60,50 @@ PackageTextViewer::MessageReceived(BMessage *msg)
void
PackageTextViewer::_InitView(const char *text, bool disclaimer)
PackageTextViewer::_InitView(const char* text, bool disclaimer)
{
fBackground = new BView(Bounds(), "background_view", 0, 0);
fBackground->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
BTextView* textView = new BTextView("text_view");
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) {
BButton *button = new BButton(BRect(0, 0, 1, 1), "accept",
B_TRANSLATE("Accept"), new BMessage(P_MSG_ACCEPT));
button->ResizeToPreferred();
defaultButton = new BButton("accept", B_TRANSLATE("Accept"),
new BMessage(P_MSG_ACCEPT));
bounds = button->Bounds();
rect.top = rect.bottom - bounds.bottom - 5.0f;
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);
BButton* decline = new BButton("decline", B_TRANSLATE("Decline"),
new BMessage(P_MSG_DECLINE));
button = new BButton(BRect(0, 0, 1, 1), "decline",
B_TRANSLATE("Decline"), new BMessage(P_MSG_DECLINE));
button->ResizeToPreferred();
bounds = button->Bounds();
rect.left -= bounds.right + 7.0f;
button->MoveTo(rect.LeftTop());
fBackground->AddChild(button);
BLayoutBuilder::Group<>(this, B_VERTICAL)
.Add(scrollView)
.AddGroup(B_HORIZONTAL)
.AddGlue()
.Add(defaultButton)
.Add(decline)
.End()
.SetInsets(B_USE_WINDOW_INSETS)
;
} else {
BButton *button = new BButton(BRect(0, 0, 1, 1), "accept",
B_TRANSLATE("Continue"), new BMessage(P_MSG_ACCEPT));
button->ResizeToPreferred();
defaultButton = new BButton("accept", B_TRANSLATE("Continue"),
new BMessage(P_MSG_ACCEPT));
bounds = button->Bounds();
rect.top = rect.bottom - bounds.bottom - 5.0f;
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);
BLayoutBuilder::Group<>(this, B_VERTICAL)
.Add(scrollView)
.AddGroup(B_HORIZONTAL)
.AddGlue()
.Add(defaultButton)
.End()
.SetInsets(B_USE_WINDOW_INSETS)
;
}
bounds = Bounds().InsetBySelf(5.0f, 5.0f);
bounds.bottom = rect.top - 6.0f;
bounds.right -= B_V_SCROLL_BAR_WIDTH;
defaultButton->MakeDefault(true);
fText = new BTextView(bounds, "text_view", BRect(0, 0, bounds.Width(),
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);
textView->SetText(text);
}
/*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
#define PACKAGETEXTVIEWER_H
#include <Window.h>
#include <View.h>
#include <TextView.h>
#include "BlockingWindow.h"
class PackageTextViewer : public BWindow {
public:
PackageTextViewer(const char *text, bool disclaimer = false);
~PackageTextViewer();
class PackageTextViewer : public BlockingWindow {
public:
PackageTextViewer(const char* text,
bool disclaimer = false);
int32 Go();
void MessageReceived(BMessage *msg);
virtual void MessageReceived(BMessage* message);
private:
void _InitView(const char *text, bool disclaimer);
BView *fBackground;
BTextView *fText;
sem_id fSemaphore;
int32 fValue;
private:
void _InitView(const char *text, bool disclaimer);
};