Skip to content

Commit

Permalink
Don't allow to set negative widths
Browse files Browse the repository at this point in the history
  • Loading branch information
Mattias1 committed Dec 27, 2024
1 parent b1687be commit 80b145a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
14 changes: 8 additions & 6 deletions AvaloniaExtensions/CanvasControlExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public static T HotKey<T>(this T control, string hotkey) where T : Button {

public static double GetWidth(this Control ctrl) => double.IsNaN(ctrl.Width) ? ctrl.Bounds.Width : ctrl.Width;
public static double GetHeight(this Control ctrl) => double.IsNaN(ctrl.Height) ? ctrl.Bounds.Height : ctrl.Height;
public static T SetWidth<T>(this T ctrl, double width) where T : Control => ctrl.Width(Math.Max(width, 2));
public static T SetHeight<T>(this T ctrl, double height) where T : Control => ctrl.Height(Math.Max(height, 2));

// --- Position relative to other controls ---
public static T RightOf<T>(this T control) where T : Control => control.RightOf(PreviousControlFor(control));
Expand Down Expand Up @@ -209,41 +211,41 @@ public static T YBottomInPanel<T>(this T control) where T : Control {
public static T StretchRightTo<T>(this T control, Control other) where T : Control {
ThrowIfPositioningRelativeToYourself(control, other);
return CanvasComponentBase.RegisterOnResizeAction(control,
() => control.Width(other.GetX() - control.GetX() - Math.Max(control.Margin.Right, other.Margin.Left)));
() => control.SetWidth(other.GetX() - control.GetX() - Math.Max(control.Margin.Right, other.Margin.Left)));
}

public static T StretchDownTo<T>(this T control) where T : Control => control.StretchDownTo(PreviousControlFor(control));
public static T StretchDownTo<T>(this T control, Control other) where T : Control {
ThrowIfPositioningRelativeToYourself(control, other);
return CanvasComponentBase.RegisterOnResizeAction(control,
() => control.Height(other.GetY() - control.GetY() - Math.Max(control.Margin.Bottom, other.Margin.Top)));
() => control.SetHeight(other.GetY() - control.GetY() - Math.Max(control.Margin.Bottom, other.Margin.Top)));
}

public static T StretchRightInPanel<T>(this T control) where T : Control {
return CanvasComponentBase.RegisterOnResizeAction(control, () => {
var canvas = CanvasComponentBase.FindCanvas(control);
control.Width(canvas.GetWidth() - control.GetX() - control.Margin.Left - control.Margin.Right);
control.SetWidth(canvas.GetWidth() - control.GetX() - control.Margin.Left - control.Margin.Right);
});
}
public static T StretchDownInPanel<T>(this T control) where T : Control {
return CanvasComponentBase.RegisterOnResizeAction(control, () => {
var canvas = CanvasComponentBase.FindCanvas(control);
control.Height(canvas.GetHeight() - control.GetY() - control.Margin.Top - control.Margin.Bottom);
control.SetHeight(canvas.GetHeight() - control.GetY() - control.Margin.Top - control.Margin.Bottom);
});
}

public static T StretchFractionRightInPanel<T>(this T control, int numerator, int denominator) where T : Control {
return CanvasComponentBase.RegisterOnResizeAction(control, () => {
var canvas = CanvasComponentBase.FindCanvas(control);
var fullWidth = canvas.GetWidth() - control.GetX() - control.Margin.Left - control.Margin.Right;
control.Width(fullWidth * numerator / denominator - control.Margin.Right * .5f);
control.SetWidth(fullWidth * numerator / denominator - control.Margin.Right * .5f);
});
}
public static T StretchFractionDownInPanel<T>(this T control, int numerator, int denominator) where T : Control {
return CanvasComponentBase.RegisterOnResizeAction(control, () => {
var canvas = CanvasComponentBase.FindCanvas(control);
var fullHeight = canvas.GetHeight() - control.GetY() - control.Margin.Top - control.Margin.Bottom;
control.Height(fullHeight * numerator / denominator - control.Margin.Bottom * .5f);
control.SetHeight(fullHeight * numerator / denominator - control.Margin.Bottom * .5f);
});
}

Expand Down
6 changes: 3 additions & 3 deletions build-example-release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ cd ExampleApp/
rm -rf publish-win-x64
rm -rf publish-linux-x64

# Build application
dotnet publish -c Release -o ./publish-win-x64 -f net8.0 -r win-x64 --self-contained /p:PublishSingleFile=true /p:PublishTrimmed=true /p:IncludeNativeLibrariesForSelfExtract=true
dotnet publish -c Release -o ./publish-linux-x64 -f net8.0 -r linux-x64 --self-contained /p:PublishSingleFile=true /p:PublishTrimmed=true /p:IncludeNativeLibrariesForSelfExtract=true
# Build application (note that you might want to disable trimming, because it could cause problems)
dotnet publish -c Release -o ./publish-win-x64 -f net8.0 -r win-x64 --self-contained /p:PublishSingleFile=true /p:IncludeNativeLibrariesForSelfExtract=true /p:PublishTrimmed=true
dotnet publish -c Release -o ./publish-linux-x64 -f net8.0 -r linux-x64 --self-contained /p:PublishSingleFile=true /p:IncludeNativeLibrariesForSelfExtract=true /p:PublishTrimmed=true

0 comments on commit 80b145a

Please sign in to comment.