Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: Colors, Spacing, and Border clean up and improvements #328

Merged
merged 6 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/actions/test/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ runs:

- name: Run build runner
shell: bash
run: melos run build_runner
run: melos run brb

- name: Install DCM
if: ${{ inputs.run-dcm }}
Expand Down
11 changes: 6 additions & 5 deletions melos.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
name: mix_workspace


packages:
- packages/**
- examples/**
Expand All @@ -14,11 +15,11 @@ categories:
- examples/**

scripts:
build_runner:
run: melos run build_runner_clean && melos exec --depends-on="build_runner" --category="mix_deps" dart run build_runner build --delete-conflicting-outputs
description: Run build_runner for projects that have it as a dependency
build_runner_clean:
run: melos exec --depends-on="build_runner" dart run build_runner clean
brb:
run: melos exec --scope="mix" -- dart run build_runner build --delete-conflicting-outputs
description: Run build_runner for the mix package
brbc:
run: melos exec --scope="mix" --depends-on="build_runner" dart run build_runner clean
description: Clean build_runner for projects that have it as a dependency
test:
run: melos exec --category="should_test" flutter test
Expand Down
19 changes: 9 additions & 10 deletions packages/mix/lib/mix.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
library mix;

export 'src/attributes/attributes.dart';
export 'src/core/core.dart';
export 'src/modifiers/modifiers.dart';
export 'src/specs/specs.dart';
export 'src/theme/theme.dart';
export 'src/variants/variants.dart';
export 'src/widgets/widgets.dart';

/// /\\\\ /\\\\ /\\\\\\\\\\\ /\\\ /\\\
/// \/\\\\\\ /\\\\\\ \/////\\\/// \///\\\ /\\\/
/// \/\\\//\\\ /\\\//\\\ \/\\\ \///\\\\\\/
Expand All @@ -26,3 +16,12 @@ export 'src/widgets/widgets.dart';
/// \/\ Generated with barrel script \ \
/// \/////////////////////////////////////////////////////

library mix;

export 'src/attributes/attributes.dart';
export 'src/core/core.dart';
export 'src/modifiers/modifiers.dart';
export 'src/specs/specs.dart';
export 'src/theme/theme.dart';
export 'src/variants/variants.dart';
export 'src/widgets/widgets.dart';
2 changes: 1 addition & 1 deletion packages/mix/lib/src/attributes/attributes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export 'border/shape_border_dto.dart';
export 'color/color_directives.dart';
export 'color/color_dto.dart';
export 'color/color_util.dart';
export 'color/material_colors_util.dart';
export 'constraints/constraints_dto.dart';
export 'decoration/decoration_dto.dart';
export 'decoration/image/decoration_image_dto.dart';
Expand All @@ -27,7 +28,6 @@ export 'scalars/scalar_util.dart';
export 'shadow/shadow_dto.dart';
export 'shadow/shadow_util.dart';
export 'spacing/edge_insets_dto.dart';
export 'spacing/spacing_dto.dart';
export 'spacing/spacing_util.dart';
export 'strut_style/strut_style_dto.dart';
export 'text_style/text_style_dto.dart';
Expand Down
215 changes: 90 additions & 125 deletions packages/mix/lib/src/attributes/border/border_dto.dart
Original file line number Diff line number Diff line change
@@ -1,150 +1,136 @@
// ignore_for_file: prefer_relative_imports,avoid-importing-entrypoint-exports
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:mix/mix.dart';
import 'package:mix_annotations/mix_annotations.dart';

import '../../../mix.dart';

part 'border_dto.g.dart';

@immutable
class BoxBorderDto extends Dto<BoxBorder> {
sealed class BoxBorderDto<T extends BoxBorder> extends Dto<T> {
final BorderSideDto? top;
final BorderSideDto? bottom;

final BorderSideDto? left;
final BorderSideDto? right;
const BoxBorderDto({this.top, this.bottom});

// Directional
final BorderSideDto? start;
final BorderSideDto? end;
/// Will try to merge two borders, the type will resolve to type of
/// `b` if its not null and `a` otherwise.
static BoxBorderDto? tryToMerge(BoxBorderDto? a, BoxBorderDto? b) {
if (b == null) return a;
if (a == null) return b;

const BoxBorderDto({
this.top,
this.bottom,
this.left,
this.right,
this.start,
this.end,
});

const BoxBorderDto.fromSide(BorderSideDto? side)
: this(top: side, bottom: side, left: side, right: side);
return a.runtimeType == b.runtimeType ? a.merge(b) : _exhaustiveMerge(a, b);
}

bool get _colorIsUniform {
final topColor = top?.color;
static B _exhaustiveMerge<A extends BoxBorderDto, B extends BoxBorderDto>(
A a,
B b,
) {
if (a.runtimeType == b.runtimeType) return a.merge(b) as B;

return left?.color == topColor &&
bottom?.color == topColor &&
right?.color == topColor;
return switch (b) {
(BorderDto g) => a._asBorder().merge(g) as B,
(BorderDirectionalDto g) => a._asBorderDirectional().merge(g) as B,
};
}

bool get _widthIsUniform {
final topWidth = top?.width;
BorderDto _asBorder() {
if (this is BorderDto) return this as BorderDto;

return left?.width == topWidth &&
bottom?.width == topWidth &&
right?.width == topWidth;
return BorderDto(top: top, bottom: bottom);
}

bool get _styleIsUniform {
final topStyle = top?.style;
BorderDirectionalDto _asBorderDirectional() {
if (this is BorderDirectionalDto) return this as BorderDirectionalDto;

return left?.style == topStyle &&
bottom?.style == topStyle &&
right?.style == topStyle;
return BorderDirectionalDto(top: top, bottom: bottom);
}

bool get _strokeAlignIsUniform {
final topStrokeAlign = top?.strokeAlign;
bool get isUniform;

return left?.strokeAlign == topStrokeAlign &&
bottom?.strokeAlign == topStrokeAlign &&
right?.strokeAlign == topStrokeAlign;
}
bool get isDirectional => this is BorderDirectionalDto;

bool get _hasStartOrEnd => start != null || end != null;
@override
BoxBorderDto<T> merge(covariant BoxBorderDto<T>? other);
}

bool get _hasLeftOrRight => left != null || right != null;
@MixableDto(generateUtility: false)
final class BorderDto extends BoxBorderDto<Border> with _$BorderDto {
final BorderSideDto? left;
final BorderSideDto? right;

bool get isUniform =>
_colorIsUniform &&
_widthIsUniform &&
_styleIsUniform &&
_strokeAlignIsUniform;
const BorderDto({super.top, super.bottom, this.left, this.right});

bool get isDirectional => _hasStartOrEnd && !_hasLeftOrRight;
const BorderDto.all(BorderSideDto side)
: this(top: side, bottom: side, left: side, right: side);

Type? checkMergeType(BoxBorderDto other) {
if (other._hasLeftOrRight || !other._hasStartOrEnd) {
return Border;
}
if (other._hasStartOrEnd || !other._hasLeftOrRight) {
return BorderDirectional;
}
const BorderDto.none() : this.all(const BorderSideDto.none());

return null;
}
const BorderDto.symmetric({
BorderSideDto? vertical,
BorderSideDto? horizontal,
}) : this(
top: horizontal,
bottom: horizontal,
left: vertical,
right: vertical,
);

@override
BoxBorderDto merge(BoxBorderDto? other) {
if (other == null) return this;
final type = checkMergeType(other);
assert(type != null, 'Cannot merge Border with BoxBorderDirectional');
if (type == Border) {
return BoxBorderDto(
top: top?.merge(other.top) ?? other.top,
bottom: bottom?.merge(other.bottom) ?? other.bottom,
left: left?.merge(other.left) ?? other.left,
right: right?.merge(other.right) ?? other.right,
);
}
const BorderDto.vertical(BorderSideDto side) : this.symmetric(vertical: side);

if (type == BorderDirectional) {
return BoxBorderDto(
top: top?.merge(other.top) ?? other.top,
bottom: bottom?.merge(other.bottom) ?? other.bottom,
start: start?.merge(other.start) ?? other.start,
end: end?.merge(other.end) ?? other.end,
);
}
const BorderDto.horizontal(BorderSideDto side)
: this.symmetric(horizontal: side);

return other;
}
@override
bool get isUniform => top == bottom && top == left && top == right;

@override
BoxBorder resolve(MixData mix) {
if (isDirectional) {
return BorderDirectional(
top: top?.resolve(mix) ?? BorderSide.none,
start: start?.resolve(mix) ?? BorderSide.none,
end: end?.resolve(mix) ?? BorderSide.none,
bottom: bottom?.resolve(mix) ?? BorderSide.none,
);
}
Border get defaultValue => const Border();
}

return Border(
top: top?.resolve(mix) ?? BorderSide.none,
right: right?.resolve(mix) ?? BorderSide.none,
bottom: bottom?.resolve(mix) ?? BorderSide.none,
left: left?.resolve(mix) ?? BorderSide.none,
);
}
@MixableDto(generateUtility: false)
final class BorderDirectionalDto extends BoxBorderDto<BorderDirectional>
with _$BorderDirectionalDto {
final BorderSideDto? start;
final BorderSideDto? end;
const BorderDirectionalDto({
super.top,
super.bottom,
this.start,
this.end,
});

const BorderDirectionalDto.all(BorderSideDto side)
: this(top: side, bottom: side, start: side, end: side);

const BorderDirectionalDto.symmetric({
BorderSideDto? vertical,
BorderSideDto? horizontal,
}) : this(
top: horizontal,
bottom: horizontal,
start: vertical,
end: vertical,
);

const BorderDirectionalDto.none() : this.all(const BorderSideDto.none());
const BorderDirectionalDto.vertical(BorderSideDto side)
: this.symmetric(vertical: side);

const BorderDirectionalDto.horizontal(BorderSideDto side)
: this.symmetric(horizontal: side);

@override
BoxBorder get defaultValue => const Border();
bool get isUniform => top == bottom && top == start && top == end;

@override
get props => [top, bottom, left, right, start, end];
BorderDirectional get defaultValue => const BorderDirectional();
}

@MixableDto()
final class BorderSideDto extends Dto<BorderSide> with _$BorderSideDto {
final ColorDto? color;
final double? width;

@MixableProperty(utilities: [
MixableUtility(properties: [(path: 'none', alias: 'none')]),
])
final BorderStyle? style;
final double? strokeAlign;

Expand All @@ -155,7 +141,8 @@ final class BorderSideDto extends Dto<BorderSide> with _$BorderSideDto {
this.width,
});

const BorderSideDto.none() : this();
const BorderSideDto.none() : this(style: BorderStyle.none, width: 0.0);

@override
BorderSide get defaultValue => const BorderSide();
}
Expand All @@ -176,25 +163,3 @@ extension BoxBorderExt on BoxBorder {
);
}
}

extension BorderDirectionalExt on BorderDirectional {
BoxBorderDto toDto() {
return BoxBorderDto(
top: top.toDto(),
bottom: bottom.toDto(),
start: start.toDto(),
end: end.toDto(),
);
}
}

extension BorderExt on Border {
BoxBorderDto toDto() {
return BoxBorderDto(
top: top.toDto(),
bottom: bottom.toDto(),
left: left.toDto(),
right: right.toDto(),
);
}
}
Loading
Loading