Some more work, edging us ever-closer to the correct solution...

This commit is contained in:
Pietro Gagliardi 2016-05-12 12:26:43 -04:00
parent 60c988100a
commit 28c9efa972
6 changed files with 140 additions and 31 deletions

View File

@ -249,16 +249,14 @@ struct uiBox {
- (void)append:(uiControl *)c stretchy:(int)stretchy - (void)append:(uiControl *)c stretchy:(int)stretchy
{ {
boxChild *bc; boxChild *bc;
NSView *childView;
NSLayoutPriority priority; NSLayoutPriority priority;
uintmax_t oldnStretchy; uintmax_t oldnStretchy;
bc = [boxChild new]; bc = [boxChild new];
bc.c = c; bc.c = c;
bc.stretchy = stretchy; bc.stretchy = stretchy;
childView = [bc view]; bc.oldPrimaryHuggingPri = uiDarwinControlHuggingPriority(uiDarwinControl(bc.c), self->primaryOrientation);
bc.oldPrimaryHuggingPri = [childView contentHuggingPriorityForOrientation:self->primaryOrientation]; bc.oldSecondaryHuggingPri = uiDarwinControlHuggingPriority(uiDarwinControl(bc.c), self->secondaryOrientation);
bc.oldSecondaryHuggingPri = [childView contentHuggingPriorityForOrientation:self->secondaryOrientation];
uiControlSetParent(bc.c, uiControl(self->b)); uiControlSetParent(bc.c, uiControl(self->b));
uiDarwinControlSetSuperview(uiDarwinControl(bc.c), self); uiDarwinControlSetSuperview(uiDarwinControl(bc.c), self);
@ -271,9 +269,9 @@ struct uiBox {
else else
// TODO will default high work? // TODO will default high work?
priority = NSLayoutPriorityRequired; priority = NSLayoutPriorityRequired;
[childView setContentHuggingPriority:priority forOrientation:self->primaryOrientation]; uiDarwinControlSetHuggingPriority(uiDarwinControl(bc.c), priority, self->primaryOrientation);
// make sure controls don't hug their secondary direction so they fill the width of the view // make sure controls don't hug their secondary direction so they fill the width of the view
[childView setContentHuggingPriority:NSLayoutPriorityDefaultLow forOrientation:self->secondaryOrientation]; uiDarwinControlSetHuggingPriority(uiDarwinControl(bc.c), NSLayoutPriorityDefaultLow, self->secondaryOrientation);
[self->children addObject:bc]; [self->children addObject:bc];
@ -281,14 +279,8 @@ struct uiBox {
if (bc.stretchy) { if (bc.stretchy) {
oldnStretchy = self->nStretchy; oldnStretchy = self->nStretchy;
self->nStretchy++; self->nStretchy++;
if (oldnStretchy == 0) { if (oldnStretchy == 0)
// TODO isolate into its own function? uiDarwinNotifyEdgeHuggingChanged(uiDarwinControl(self->b));
uiControl *parent;
parent = uiControlParent(uiControl(self->b));
if (parent != NULL)
uiDarwinControlChildEdgeHuggingChanged(uiDarwinControl(parent));
}
} }
[bc release]; // we don't need the initial reference now [bc release]; // we don't need the initial reference now
@ -297,33 +289,25 @@ struct uiBox {
- (void)delete:(uintmax_t)n - (void)delete:(uintmax_t)n
{ {
boxChild *bc; boxChild *bc;
NSView *removedView;
int stretchy; int stretchy;
// TODO separate into a method? // TODO separate into a method?
bc = (boxChild *) [self->children objectAtIndex:n]; bc = (boxChild *) [self->children objectAtIndex:n];
removedView = [bc view];
stretchy = bc.stretchy; stretchy = bc.stretchy;
uiControlSetParent(bc.c, NULL); uiControlSetParent(bc.c, NULL);
uiDarwinControlSetSuperview(uiDarwinControl(bc.c), nil); uiDarwinControlSetSuperview(uiDarwinControl(bc.c), nil);
[removedView setContentHuggingPriority:bc.oldPrimaryHuggingPri forOrientation:self->primaryOrientation]; uiDarwinControlSetHuggingPriority(uiDarwinControl(bc.c), bc.oldPrimaryHuggingPri, self->primaryOrientation);
[removedView setContentHuggingPriority:bc.oldSecondaryHuggingPri forOrientation:self->secondaryOrientation]; uiDarwinControlSetHuggingPriority(uiDarwinControl(bc.c), bc.oldSecondaryHuggingPri, self->secondaryOrientation);
[self->children removeObjectAtIndex:n]; [self->children removeObjectAtIndex:n];
[self establishOurConstraints]; [self establishOurConstraints];
if (stretchy) { if (stretchy) {
self->nStretchy--; self->nStretchy--;
if (self->nStretchy == 0) { if (self->nStretchy == 0)
// TODO isolate into its own function? uiDarwinNotifyEdgeHuggingChanged(uiDarwinControl(self->b));
uiControl *parent;
parent = uiControlParent(uiControl(self->b));
if (parent != NULL)
uiDarwinControlChildEdgeHuggingChanged(uiDarwinControl(parent));
}
} }
} }
@ -412,6 +396,9 @@ static void uiBoxChildEdgeHuggingChanged(uiDarwinControl *c)
[b->view establishOurConstraints]; [b->view establishOurConstraints];
} }
uiDarwinControlDefaultHuggingPriority(uiBox, view)
uiDarwinControlDefaultSetHuggingPriority(uiBox, view)
void uiBoxAppend(uiBox *b, uiControl *c, int stretchy) void uiBoxAppend(uiBox *b, uiControl *c, int stretchy)
{ {
[b->view append:c stretchy:stretchy]; [b->view append:c stretchy:stretchy];

View File

@ -26,6 +26,16 @@ void uiDarwinControlChildEdgeHuggingChanged(uiDarwinControl *c)
(*(c->ChildEdgeHuggingChanged))(c); (*(c->ChildEdgeHuggingChanged))(c);
} }
NSLayoutPriority uiDarwinControlHuggingPriority(uiDarwinControl *c, NSLayoutConstraintOrientation orientation)
{
return (*(c->HuggingPriority))(c, orientation);
}
void uiDarwinControlSetHuggingPriority(uiDarwinControl *c, NSLayoutPriority priority, NSLayoutConstraintOrientation orientation)
{
(*(c->SetHuggingPriority))(c, priority, orientation);
}
void uiDarwinSetControlFont(NSControl *c, NSControlSize size) void uiDarwinSetControlFont(NSControl *c, NSControlSize size)
{ {
[c setFont:[NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:size]]]; [c setFont:[NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:size]]];
@ -49,3 +59,12 @@ BOOL uiDarwinShouldStopSyncEnableState(uiDarwinControl *c, BOOL enabled)
return YES; return YES;
return NO; return NO;
} }
void uiDarwinNotifyEdgeHuggingChanged(uiDarwinControl *c)
{
uiControl *parent;
parent = uiControlParent(uiControl(c));
if (parent != NULL)
uiDarwinControlChildEdgeHuggingChanged(uiDarwinControl(parent));
}

View File

@ -9,6 +9,8 @@ struct uiGroup {
uiControl *child; uiControl *child;
int margined; int margined;
struct singleChildConstraints constraints; struct singleChildConstraints constraints;
NSLayoutPriority horzHuggingPri;
NSLayoutPriority vertHuggingPri;
}; };
static void removeConstraints(uiGroup *g) static void removeConstraints(uiGroup *g)
@ -69,8 +71,21 @@ static void groupRelayout(uiGroup *g)
@"uiGroup"); @"uiGroup");
} }
uiDarwinControlDefaultHugsTrailingEdge(uiGroup, box) // TODO rename these since I'm starting to get confused by what they mean by hugging
uiDarwinControlDefaultHugsBottom(uiGroup, box) BOOL uiGroupHugsTrailingEdge(uiDarwinControl *c)
{
uiGroup *g = uiGroup(c);
// TODO make a function?
return g->horzHuggingPri < NSLayoutPriorityWindowSizeStayPut;
}
BOOL uiGroupHugsBottom(uiDarwinControl *c)
{
uiGroup *g = uiGroup(c);
return g->vertHuggingPri < NSLayoutPriorityWindowSizeStayPut;
}
static void uiGroupChildEdgeHuggingChanged(uiDarwinControl *c) static void uiGroupChildEdgeHuggingChanged(uiDarwinControl *c)
{ {
@ -79,6 +94,26 @@ static void uiGroupChildEdgeHuggingChanged(uiDarwinControl *c)
groupRelayout(g); groupRelayout(g);
} }
static NSLayoutPriority uiGroupHuggingPriority(uiDarwinControl *c, NSLayoutConstraintOrientation orientation)
{
uiGroup *g = uiGroup(c);
if (orientation == NSLayoutConstraintOrientationHorizontal)
return g->horzHuggingPri;
return g->vertHuggingPri;
}
static void uiGroupSetHuggingPriority(uiDarwinControl *c, NSLayoutPriority priority, NSLayoutConstraintOrientation orientation)
{
uiGroup *g = uiGroup(c);
if (orientation == NSLayoutConstraintOrientationHorizontal)
g->horzHuggingPri = priority;
else
g->vertHuggingPri = priority;
uiDarwinNotifyEdgeHuggingChanged(uiDarwinControl(g));
}
char *uiGroupTitle(uiGroup *g) char *uiGroupTitle(uiGroup *g)
{ {
return uiDarwinNSStringToText([g->box title]); return uiDarwinNSStringToText([g->box title]);
@ -138,5 +173,9 @@ uiGroup *uiNewGroup(const char *title)
// we can't use uiDarwinSetControlFont() because the selector is different // we can't use uiDarwinSetControlFont() because the selector is different
[g->box setTitleFont:[NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:NSSmallControlSize]]]; [g->box setTitleFont:[NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:NSSmallControlSize]]];
// default to low hugging to not hug edges
g->horzHuggingPri = NSLayoutPriorityDefaultLow;
g->vertHuggingPri = NSLayoutPriorityDefaultLow;
return g; return g;
} }

View File

@ -20,6 +20,8 @@ struct uiTab {
uiDarwinControl c; uiDarwinControl c;
NSTabView *tabview; NSTabView *tabview;
NSMutableArray *pages; NSMutableArray *pages;
NSLayoutPriority horzHuggingPri;
NSLayoutPriority vertHuggingPri;
}; };
@implementation tabPage @implementation tabPage
@ -124,8 +126,20 @@ static void tabRelayout(uiTab *t)
[page establishChildConstraints]; [page establishChildConstraints];
} }
uiDarwinControlDefaultHugsTrailingEdge(uiTab, tabview) BOOL uiTabHugsTrailingEdge(uiDarwinControl *c)
uiDarwinControlDefaultHugsBottom(uiTab, tabview) {
uiTab *t = uiTab(c);
// TODO make a function?
return t->horzHuggingPri < NSLayoutPriorityWindowSizeStayPut;
}
BOOL uiTabHugsBottom(uiDarwinControl *c)
{
uiTab *t = uiTab(c);
return t->vertHuggingPri < NSLayoutPriorityWindowSizeStayPut;
}
static void uiTabChildEdgeHuggingChanged(uiDarwinControl *c) static void uiTabChildEdgeHuggingChanged(uiDarwinControl *c)
{ {
@ -134,6 +148,26 @@ static void uiTabChildEdgeHuggingChanged(uiDarwinControl *c)
tabRelayout(t); tabRelayout(t);
} }
static NSLayoutPriority uiTabHuggingPriority(uiDarwinControl *c, NSLayoutConstraintOrientation orientation)
{
uiTab *t = uiTab(c);
if (orientation == NSLayoutConstraintOrientationHorizontal)
return t->horzHuggingPri;
return t->vertHuggingPri;
}
static void uiTabSetHuggingPriority(uiDarwinControl *c, NSLayoutPriority priority, NSLayoutConstraintOrientation orientation)
{
uiTab *t = uiTab(c);
if (orientation == NSLayoutConstraintOrientationHorizontal)
t->horzHuggingPri = priority;
else
t->vertHuggingPri = priority;
uiDarwinNotifyEdgeHuggingChanged(uiDarwinControl(t));
}
void uiTabAppend(uiTab *t, const char *name, uiControl *child) void uiTabAppend(uiTab *t, const char *name, uiControl *child)
{ {
uiTabInsertAt(t, name, [t->pages count], child); uiTabInsertAt(t, name, [t->pages count], child);
@ -225,5 +259,9 @@ uiTab *uiNewTab(void)
t->pages = [NSMutableArray new]; t->pages = [NSMutableArray new];
// default to low hugging to not hug edges
t->horzHuggingPri = NSLayoutPriorityDefaultLow;
t->vertHuggingPri = NSLayoutPriorityDefaultLow;
return t; return t;
} }

View File

@ -177,6 +177,11 @@ static void uiWindowChildEdgeHuggingChanged(uiDarwinControl *c)
windowRelayout(w); windowRelayout(w);
} }
// TODO
uiDarwinControlDefaultHuggingPriority(uiWindow, window)
uiDarwinControlDefaultSetHuggingPriority(uiWindow, window)
// end TODO
char *uiWindowTitle(uiWindow *w) char *uiWindowTitle(uiWindow *w)
{ {
return uiDarwinNSStringToText([w->window title]); return uiDarwinNSStringToText([w->window title]);

View File

@ -22,6 +22,8 @@ struct uiDarwinControl {
BOOL (*HugsTrailingEdge)(uiDarwinControl *); BOOL (*HugsTrailingEdge)(uiDarwinControl *);
BOOL (*HugsBottom)(uiDarwinControl *); BOOL (*HugsBottom)(uiDarwinControl *);
void (*ChildEdgeHuggingChanged)(uiDarwinControl *); void (*ChildEdgeHuggingChanged)(uiDarwinControl *);
NSLayoutPriority (*HuggingPriority)(uiDarwinControl *, NSLayoutConstraintOrientation);
void (*SetHuggingPriority)(uiDarwinControl *, NSLayoutPriority, NSLayoutConstraintOrientation);
}; };
#define uiDarwinControl(this) ((uiDarwinControl *) (this)) #define uiDarwinControl(this) ((uiDarwinControl *) (this))
// TODO document // TODO document
@ -30,6 +32,8 @@ _UI_EXTERN void uiDarwinControlSetSuperview(uiDarwinControl *, NSView *);
_UI_EXTERN BOOL uiDarwinControlHugsTrailingEdge(uiDarwinControl *); _UI_EXTERN BOOL uiDarwinControlHugsTrailingEdge(uiDarwinControl *);
_UI_EXTERN BOOL uiDarwinControlHugsBottom(uiDarwinControl *); _UI_EXTERN BOOL uiDarwinControlHugsBottom(uiDarwinControl *);
_UI_EXTERN void uiDarwinControlChildEdgeHuggingChanged(uiDarwinControl *); _UI_EXTERN void uiDarwinControlChildEdgeHuggingChanged(uiDarwinControl *);
_UI_EXTERN NSLayoutPriority uiDarwinControlHuggingPriority(uiDarwinControl *, NSLayoutConstraintOrientation);
_UI_EXTERN void uiDarwinControlSetHuggingPriority(uiDarwinControl *, NSLayoutPriority, NSLayoutConstraintOrientation);
#define uiDarwinControlDefaultDestroy(type, handlefield) \ #define uiDarwinControlDefaultDestroy(type, handlefield) \
static void type ## Destroy(uiControl *c) \ static void type ## Destroy(uiControl *c) \
@ -125,6 +129,16 @@ _UI_EXTERN void uiDarwinControlChildEdgeHuggingChanged(uiDarwinControl *);
{ \ { \
/* do nothing */ \ /* do nothing */ \
} }
#define uiDarwinControlDefaultHuggingPriority(type, handlefield) \
static NSLayoutPriority type ## HuggingPriority(uiDarwinControl *c, NSLayoutConstraintOrientation orientation) \
{ \
return [type(c)->handlefield contentHuggingPriorityForOrientation:orientation]; \
}
#define uiDarwinControlDefaultSetHuggingPriority(type, handlefield) \
static void type ## SetHuggingPriority(uiDarwinControl *c, NSLayoutPriority priority, NSLayoutConstraintOrientation orientation) \
{ \
[type(c)->handlefield setContentHuggingPriority:priority forOrientation:orientation]; \
}
#define uiDarwinControlAllDefaultsExceptDestroy(type, handlefield) \ #define uiDarwinControlAllDefaultsExceptDestroy(type, handlefield) \
uiDarwinControlDefaultHandle(type, handlefield) \ uiDarwinControlDefaultHandle(type, handlefield) \
@ -141,7 +155,9 @@ _UI_EXTERN void uiDarwinControlChildEdgeHuggingChanged(uiDarwinControl *);
uiDarwinControlDefaultSetSuperview(type, handlefield) \ uiDarwinControlDefaultSetSuperview(type, handlefield) \
uiDarwinControlDefaultHugsTrailingEdge(type, handlefield) \ uiDarwinControlDefaultHugsTrailingEdge(type, handlefield) \
uiDarwinControlDefaultHugsBottom(type, handlefield) \ uiDarwinControlDefaultHugsBottom(type, handlefield) \
uiDarwinControlDefaultChildEdgeHuggingChanged(type, handlefield) uiDarwinControlDefaultChildEdgeHuggingChanged(type, handlefield) \
uiDarwinControlDefaultHuggingPriority(type, handlefield) \
uiDarwinControlDefaultSetHuggingPriority(type, handlefield)
#define uiDarwinControlAllDefaults(type, handlefield) \ #define uiDarwinControlAllDefaults(type, handlefield) \
uiDarwinControlDefaultDestroy(type, handlefield) \ uiDarwinControlDefaultDestroy(type, handlefield) \
@ -166,6 +182,8 @@ _UI_EXTERN void uiDarwinControlChildEdgeHuggingChanged(uiDarwinControl *);
uiDarwinControl(var)->HugsTrailingEdge = type ## HugsTrailingEdge; \ uiDarwinControl(var)->HugsTrailingEdge = type ## HugsTrailingEdge; \
uiDarwinControl(var)->HugsBottom = type ## HugsBottom; \ uiDarwinControl(var)->HugsBottom = type ## HugsBottom; \
uiDarwinControl(var)->ChildEdgeHuggingChanged = type ## ChildEdgeHuggingChanged; \ uiDarwinControl(var)->ChildEdgeHuggingChanged = type ## ChildEdgeHuggingChanged; \
uiDarwinControl(var)->HuggingPriority = type ## HuggingPriority; \
uiDarwinControl(var)->SetHuggingPriority = type ## SetHuggingPriority; \
uiDarwinControl(var)->visible = YES; \ uiDarwinControl(var)->visible = YES; \
uiDarwinControl(var)->enabled = YES; uiDarwinControl(var)->enabled = YES;
// TODO document // TODO document
@ -180,6 +198,9 @@ _UI_EXTERN char *uiDarwinNSStringToText(NSString *);
// TODO document // TODO document
_UI_EXTERN BOOL uiDarwinShouldStopSyncEnableState(uiDarwinControl *, BOOL); _UI_EXTERN BOOL uiDarwinShouldStopSyncEnableState(uiDarwinControl *, BOOL);
// TODO document
_UI_EXTERN void uiDarwinNotifyEdgeHuggingChanged(uiDarwinControl *);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif