Started implementing the new logic; darwin/window.m implemented. darwin/group.m and darwin/tab.m will be similar, so they use common code here (but this is NOT child.m all over again; this is ONLY for layout!).

This commit is contained in:
Pietro Gagliardi 2016-05-11 18:06:29 -04:00
parent 73eed9289c
commit 178c0301c2
3 changed files with 127 additions and 7 deletions

View File

@ -17,3 +17,91 @@ NSLayoutConstraint *mkConstraint(id view1, NSLayoutAttribute attr1, NSLayoutRela
[((id) constraint) setIdentifier:desc];
return constraint;
}
static CGFloat margins(int margined)
{
if (!margined)
return 0.0;
return 20.0; // TODO named constant
}
void singleChildConstraintsEstablish(struct singleChildConstraints *c, NSView *contentView, NSView *childView, BOOL hugsTrailing, BOOL hugsBottom, int margined, NSString *desc)
{
CGFloat margin;
NSLayoutRelation relation;
margin = margins(margined);
c->leadingConstraint = mkConstraint(contentView, NSLayoutAttributeLeading,
NSLayoutRelationEqual,
childView, NSLayoutAttributeLeading,
1, margin,
[desc stringByAppendingString:@" leading constraint"]);
[contentView addConstraint:c->leadingConstraint];
c->topConstraint = mkConstraint(contentView, NSLayoutAttributeTop,
NSLayoutRelationEqual,
childView, NSLayoutAttributeTop,
1, margin,
[desc stringByAppendingString:@" top constraint"]);
[contentView addConstraint:c->topConstraint];
relation = NSLayoutRelationGreaterOrEqual;
if (hugsTrailing)
relation = NSLayoutRelationEqual;
c->leadingConstraint = mkConstraint(contentView, NSLayoutAttributeTrailing,
relation,
childView, NSLayoutAttributeTrailing,
1, -margin,
[desc stringByAppendingString:@" trailing constraint"]);
[contentView addConstraint:c->trailingConstraint];
relation = NSLayoutRelationGreaterOrEqual;
if (hugsBottom)
relation = NSLayoutRelationEqual;
c->leadingConstraint = mkConstraint(contentView, NSLayoutAttributeBottom,
relation,
childView, NSLayoutAttributeBottom,
1, -margin,
[desc stringByAppendingString:@" bottom constraint"]);
[contentView addConstraint:c->leadingConstraint];
}
void singleChildConstraintsRemove(struct singleChildConstraints *c, NSView *cv)
{
if (c->leadingConstraint != nil) {
[cv removeConstraint:c->leadingConstraint];
[c->leadingConstraint release];
c->leadingConstraint = nil;
}
if (c->topConstraint != nil) {
[cv removeConstraint:c->topConstraint];
[c->topConstraint release];
c->topConstraint = nil;
}
if (c->trailingConstraint != nil) {
[cv removeConstraint:c->trailingConstraint];
[c->trailingConstraint release];
c->trailingConstraint = nil;
}
if (c->bottomConstraint != nil) {
[cv removeConstraint:c->bottomConstraint];
[c->bottomConstraint release];
c->bottomConstraint = nil;
}
}
void singleChildConstraintsSetMargined(struct singleChildConstraints *c, int margined)
{
CGFloat margin;
margin = margins(margined);
if (c->leadingConstraint != nil)
[c->leadingConstraint setConstant:margin];
if (c->topConstraint != nil)
[c->topConstraint setConstant:margin];
if (c->trailingConstraint != nil)
[c->trailingConstraint setConstant:-margin];
if (c->bottomConstraint != nil)
[c->bottomConstraint setConstant:-margin];
}

View File

@ -57,6 +57,15 @@ extern void uninitAlloc(void);
// autolayout.m
extern NSLayoutConstraint *mkConstraint(id view1, NSLayoutAttribute attr1, NSLayoutRelation relation, id view2, NSLayoutAttribute attr2, CGFloat multiplier, CGFloat c, NSString *desc);
struct singleChildConstraints {
NSLayoutConstraint *leadingConstraint;
NSLayoutConstraint *topConstraint;
NSLayoutConstraint *trailingConstraint;
NSLayoutConstraint *bottomConstraint;
};
extern void singleChildConstraintsEstablish(struct singleChildConstraints *c, NSView *contentView, NSView *childView, BOOL hugsTrailing, BOOL hugsBottom, int margined, NSString *desc);
extern void singleChildConstraintsRemove(struct singleChildConstraints *c, NSView *cv);
extern void singleChildConstraintsSetMargined(struct singleChildConstraints *c, int margined);
// map.m
extern struct mapTable *newMap(void);

View File

@ -8,6 +8,8 @@ struct uiWindow {
int margined;
int (*onClosing)(uiWindow *, void *);
void *onClosingData;
struct singleChildConstraints constraints;
};
@interface windowDelegateClass : NSObject<NSWindowDelegate> {
@ -71,6 +73,14 @@ struct uiWindow {
static windowDelegateClass *windowDelegate = nil;
static void removeConstraints(uiWindow *w)
{
NSView *cv;
cv = [w->window contentView];
singleChildConstraintsRemove(&(w->constraints), cv);
}
static void uiWindowDestroy(uiControl *c)
{
uiWindow *w = uiWindow(c);
@ -78,6 +88,7 @@ static void uiWindowDestroy(uiControl *c)
// hide the window
[w->window orderOut:w->window];
removeConstraints(w);
if (w->child != NULL) {
childView = (NSView *) uiControlHandle(w->child);
[childView removeFromSuperview];
@ -143,19 +154,30 @@ static void uiWindowSetSuperview(uiDarwinControl *c, NSView *superview)
static void windowRelayout(uiWindow *w)
{
uiDarwinControl *cc;
NSView *childView;
NSView *contentView;
removeConstraints(w);
if (w->child == NULL)
return;
cc = uiDarwinControl(w->child);
childView = (NSView *) uiControlHandle(w->child);
contentView = [w->window contentView];
// first relayout the child
//TODO (*(cc->Relayout))(cc);
// now relayout ourselves
//TODO layoutSingleView(contentView, childView, w->margined, @"uiWindow");
singleChildConstraintsEstablish(&(w->constraints),
contentView, childView,
uiDarwinControlHugsTrailingEdge(uiDarwinControl(w->child)),
uiDarwinControlHugsBottom(uiDarwinControl(w->child)),
w->margined,
@"uiWindow");
}
uiDarwinControlDefaultHugsTrailingEdge(uiWindow, window)
uiDarwinControlDefaultHugsBottom(uiWindow, window)
static void uiWindowChildEdgeHuggingChanged(uiDarwinControl *c)
{
uiWindow *w = uiWindow(c);
windowRelayout(w);
}
char *uiWindowTitle(uiWindow *w)
@ -201,7 +223,8 @@ int uiWindowMargined(uiWindow *w)
void uiWindowSetMargined(uiWindow *w, int margined)
{
w->margined = margined;
windowRelayout(w);
singleChildConstraintsSetMargined(&(w->constraints), w->margined);
// TODO issue a relayout command?
}
static int defaultOnClosing(uiWindow *w, void *data)