Skip to content

Commit

Permalink
refactor: dispose windows on close
Browse files Browse the repository at this point in the history
- Try and ensure that the correct port is set when drawing controls to avoid weird issues
  • Loading branch information
NoxHarmonium committed Sep 3, 2021
1 parent e0ae89d commit 3120fb6
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 35 deletions.
21 changes: 18 additions & 3 deletions macbrew-ui/macbrew.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ static void HandleZoomWindow(WindowPtr thisWindow, EventRecord *event, short zoo

static void HandleGrowWindow(WindowPtr thisWindow, EventRecord *event, short windowKind)
{
Rect oldViewRect = thisWindow->portRect, limitRect;
Rect limitRect;
long growSize;

SetRect(&limitRect, kMinWindowSize, kMinWindowSize, kMaxWindowSize, kMaxWindowSize);
Expand All @@ -52,7 +52,6 @@ static void HandleGrowWindow(WindowPtr thisWindow, EventRecord *event, short win

if (growSize != 0)
{
Rect newViewRect;
// Do the actual resize
SizeWindow(thisWindow, LoWord(growSize), HiWord(growSize), TRUE);

Expand Down Expand Up @@ -115,7 +114,18 @@ static void HandleMouseDown(EventRecord *theEvent)
if (
TrackGoAway(theWindow, theEvent->where))
{
HideWindow(theWindow);
if (windowKind == kViewSessionWindowId)
{
SessionViewWindowDestroy(theWindow);
}
else if (windowKind == kViewStepsWindowId)
{
StepsViewWindowDestroy(theWindow);
}
else
{
HideWindow(theWindow);
}
}
break;

Expand All @@ -139,6 +149,7 @@ static void HandleEvent(void)
int ok;
EventRecord theEvent;
WindowPtr theWindow;
GrafPtr savePort;

HiliteMenu(0);
SystemTask(); /* Handle desk accessories */
Expand All @@ -151,6 +162,9 @@ static void HandleEvent(void)
theWindow = FrontWindow();
windowKind = ((WindowPeek)theWindow)->windowKind;

GetPort(&savePort);
SetPort(theWindow);

switch (theEvent.what)
{
case mouseDown:
Expand Down Expand Up @@ -203,6 +217,7 @@ static void HandleEvent(void)
}
break;
}
SetPort(savePort);
}
}

Expand Down
1 change: 0 additions & 1 deletion macbrew-ui/mbDSessionList.c
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,6 @@ static Boolean SessionListControlMouseDown(DialogPtr theDialog, EventRecord theE
Boolean itemSelected = false;
const SessionListDialogState *dialogState = SessionListDialogLockState(theDialog);
const ListRec *sessionList = SessionListControlLock(dialogState);
ControlHandle selectedControl;

SetPort(sessionList->port);
GlobalToLocal(&theEvent.where);
Expand Down
4 changes: 0 additions & 4 deletions macbrew-ui/mbDataManager.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,6 @@ static void ReadBrewSessionStep(ResponseReader *reader, Handle *outHandle)

HLock(handle);

// StringHandle description;
// short time;
// BrewSessionStepPhase phase;

brewSessionStep = (BrewSessionStep *)*handle;

ReadString(reader, &brewSessionStep->description);
Expand Down
8 changes: 8 additions & 0 deletions macbrew-ui/mbMenus.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ void HandleMenu(long mSelect)

FetchFermentationData((*selectedSession)->id, &fermentationData);
SessionViewSetFermentationData(viewSessionWindow, fermentationData);

// TODO: We own fermentationData and brewSession so we should probably clean them up
// but we don't know when the window is destroyed so should we get
// transfer ownership to the window and let it clean it up?
break;
}
case quitItem:
Expand All @@ -123,6 +127,10 @@ void HandleMenu(long mSelect)

FetchBrewSessionSteps((*selectedSession)->id, &sessionStepsHandle);
StepsViewSetSteps(viewStepsWindow, sessionStepsHandle);

// TODO: We own sessionStepsHandle so we should probably clean it up
// but we don't know when the window is destroyed so should we get
// transfer ownership to the window and let it clean it up?
break;
}
break;
Expand Down
2 changes: 1 addition & 1 deletion macbrew-ui/mbSerial.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// as many bytes as it sends to cancel out the echo
//
// Note: This needs to be set to zero when emulating in Basilisk II because there is no fake echo there
#define SUPRESS_ECHO 0
#define SUPRESS_ECHO 1
#define kChecksumBytes 4
// Accounts for \r\n on every response
#define kSuffixSize 2
Expand Down
72 changes: 58 additions & 14 deletions macbrew-ui/mbWViewSession.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,24 @@
#include "mbConstants.h"
#include "mbWViewSession.h"
#include "mbTypes.h"
#include "mbUtil.h"

typedef struct ViewSessionWindowState
{
BrewSessionHandle brewSessionHandle;
FermentationDataHandle fermentationDataHandle;
GrafPtr previousPort;
} ViewSessionWindowState;

static void DrawStringHandle(StringHandle stringHandle);
static void ViewSessionWindowInitState(WindowPtr theWindow);
static ViewSessionWindowState *ViewSessionWindowLockState(WindowPtr theWindow);
static void *ViewSessionWindowUnlockState(WindowPtr theWindow);
static void DrawRow(ConstStr255Param title, StringHandle value, short rowNum);
static void SetupQuickDraw(WindowPtr window);
static void DrawSessionInfo(WindowPtr window, BrewSessionHandle brewSessionHandle);
static void DrawGraph(WindowPtr window, FermentationDataHandle fermentationDataHandle);
static void SetupQuickDraw(WindowPtr window, ViewSessionWindowState *windowState);
static void CleanupQuickDraw(ViewSessionWindowState *windowState);
static void DrawSessionInfo(WindowPtr window, ViewSessionWindowState *windowState);
static void DrawGraph(WindowPtr window, ViewSessionWindowState *windowState);

// TODO: Maybe move to a utils file?
static void DrawStringHandle(StringHandle stringHandle)
Expand All @@ -38,6 +41,11 @@ static ViewSessionWindowState *ViewSessionWindowLockState(WindowPtr theWindow)
{
Handle viewSessionWindowStateHandle = (Handle)GetWRefCon(theWindow);

if (viewSessionWindowStateHandle == NULL)
{
Panic("\pCannot lock state before calling ViewSessionWindowInitState!");
}

HLock(viewSessionWindowStateHandle);
return (ViewSessionWindowState *)*viewSessionWindowStateHandle;
}
Expand All @@ -61,45 +69,63 @@ static void DrawRow(ConstStr255Param title, StringHandle value, short rowNum)
DrawStringHandle(value);
}

static void SetupQuickDraw(WindowPtr window)
static void SetupQuickDraw(WindowPtr window, ViewSessionWindowState *windowState)
{
if (windowState->previousPort != NULL)
{
Panic("\pSetupQuickDraw called twice without a call to CleanupQuickDraw");
}

GetPort(&windowState->previousPort);
// Setup QuickDraw
SetPort(window);
TextFont(geneva);
TextSize(12);
PenNormal();
}

static void DrawSessionInfo(WindowPtr window, BrewSessionHandle brewSessionHandle)
static void CleanupQuickDraw(ViewSessionWindowState *windowState)
{
// Restore the port that was set before QuickDraw was set up for this window
// This function should always be called after SetupQuickDraw or it will panic
SetPort(windowState->previousPort);
windowState->previousPort = NULL;
}

static void DrawSessionInfo(WindowPtr window, ViewSessionWindowState *windowState)
{
unsigned short rowNum = 1;
BrewSessionHandle brewSessionHandle = windowState->brewSessionHandle;
BrewSession *brewSession = NULL;

HLock((Handle)brewSessionHandle);
brewSession = *brewSessionHandle;

SetupQuickDraw(window, windowState);

HLock((Handle)brewSession->recipe_title);
// Set title to demonstrate its working for now
SetWTitle(window, *(brewSession->recipe_title));
HUnlock((Handle)brewSession->recipe_title);

SetupQuickDraw(window);

DrawRow("\pCreated At:", brewSession->created_at, rowNum++);
DrawRow("\pBatch Code:", brewSession->batch_code, rowNum++);
DrawRow("\pPhase:", brewSession->phase, rowNum++);
DrawRow("\pStyle:", brewSession->style_name, rowNum++);

CleanupQuickDraw(windowState);

HUnlock((Handle)brewSessionHandle);
}

static void DrawGraph(WindowPtr window, FermentationDataHandle fermentationDataHandle)
static void DrawGraph(WindowPtr window, ViewSessionWindowState *windowState)
{
FermentationDataHandle fermentationDataHandle = windowState->fermentationDataHandle;
FermentationData *fermentationData = NULL;
Rect graphFrame;
unsigned short i, pointSize, maxTemp = 0, maxGravity = 0;

SetupQuickDraw(window);
SetupQuickDraw(window, windowState);

HLock((Handle)fermentationDataHandle);
fermentationData = *fermentationDataHandle;
Expand Down Expand Up @@ -136,6 +162,13 @@ static void DrawGraph(WindowPtr window, FermentationDataHandle fermentationDataH
HUnlock(dataPointHandle);
}

if (maxTemp == 0 || maxGravity == 0)
{
HUnlock((Handle)fermentationDataHandle);
// Protect against divide by zero crashes
return;
}

// TODO: Labels for the graph
// TODO: Legend for the graph
// TODO: Extract things into functions for DRY
Expand Down Expand Up @@ -167,6 +200,8 @@ static void DrawGraph(WindowPtr window, FermentationDataHandle fermentationDataH
HUnlock(dataPointHandle);
}

CleanupQuickDraw(windowState);

HUnlock((Handle)fermentationDataHandle);
}

Expand All @@ -177,7 +212,7 @@ WindowPtr SessionViewWindowSetUp(void)

ViewSessionWindowInitState(viewSessionWindow);

SetPort(viewSessionWindow);
// SetPort(viewSessionWindow);

return viewSessionWindow;
}
Expand All @@ -186,6 +221,15 @@ void SessionViewWindowDestroy(WindowPtr window)
{
if (window != NULL)
{
// Clean up the state record
Handle viewSessionWindowStateHandle = (Handle)GetWRefCon(window);
if (viewSessionWindowStateHandle != NULL)
{
DisposeHandle(viewSessionWindowStateHandle);
SetWRefCon(window, (long)NULL);
}

// DisposeWindow automatically cleans up all the controls by calling DisposeControl for us
DisposeWindow(window);
window = NULL;
}
Expand All @@ -196,7 +240,7 @@ void SessionViewSetSession(WindowPtr window, BrewSessionHandle brewSessionHandle
ViewSessionWindowState *windowState = ViewSessionWindowLockState(window);
windowState->brewSessionHandle = brewSessionHandle;

DrawSessionInfo(window, brewSessionHandle);
DrawSessionInfo(window, windowState);

ViewSessionWindowUnlockState(window);
}
Expand All @@ -206,7 +250,7 @@ void SessionViewSetFermentationData(WindowPtr window, struct FermentationData **
ViewSessionWindowState *windowState = ViewSessionWindowLockState(window);
windowState->fermentationDataHandle = fermentationDataHandle;

DrawGraph(window, fermentationDataHandle);
DrawGraph(window, windowState);

ViewSessionWindowUnlockState(window);
}
Expand All @@ -219,12 +263,12 @@ void SessionViewUpdate(WindowPtr window)

if (brewSessionHandle != NULL)
{
DrawSessionInfo(window, windowState->brewSessionHandle);
DrawSessionInfo(window, windowState);
}

if (fermentationDataHandle != NULL)
{
DrawGraph(window, windowState->fermentationDataHandle);
DrawGraph(window, windowState);
}

ViewSessionWindowUnlockState(window);
Expand Down
Loading

0 comments on commit 3120fb6

Please sign in to comment.