about summary refs log tree commit diff
path: root/MatrixRoomUtils.Web/Shared/ModalWindow.razor
blob: beb7198def3f04b52b15bd32c26c4d9f51fe5fc1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
@using LibMatrix.Extensions
<div class="r-modal" style="top: @(_y)px; left: @(_x)px;">
    <div class="titlebar" @onmousedown="MouseDown" @onmouseup="MouseUp" @onmousemove="MouseMove" @onmouseleave="MouseMove">
        <b class="title" @ref="_titleRef">@Title</b>
        <button class="btnclose" @onclick="OnCloseClicked">X</button>
        <button class="btncollapse" @onclick="@(() => Collapsed = !Collapsed)">_</button>
    </div>
    <div class="r-modal-content" style="@((Collapsed ? "height: 0px;" : "") + $"min-width: {MinWidth}px;")">
        @ChildContent
    </div>
</div>

@code {

    [Parameter]
    public RenderFragment? ChildContent { get; set; }

    [Parameter]
    public string Title { get; set; } = "Untitled window";

    [Parameter]
    public double X { get; set; } = 60;

    [Parameter]
    public double Y { get; set; } = 60;

    [Parameter]
    public double MinWidth { get; set; } = 100;

    [Parameter]
    public Action OnCloseClicked { get; set; }

    [Parameter]
    public bool Collapsed { get; set; } = false;

    private ElementReference _titleRef;

    private double _x = 60;
    private double _y = 60;

    protected override async Task OnInitializedAsync() {
        _x = X;
        _y = Y;
        await base.OnInitializedAsync();
    }

    protected override async Task OnAfterRenderAsync(bool firstRender) {
    //set minwidth to title width
        MinWidth = await JSRuntime.InvokeAsync<int>("getWidth", _titleRef) + 75;
        await base.OnAfterRenderAsync(firstRender);
    }

    private void WindowDrag(DragEventArgs obj) {
        Console.WriteLine("Drag: " + obj.ToJson());

        _x += obj.MovementX;
        _y += obj.MovementY;

        StateHasChanged();
    }

    private bool isDragging = false;
    private double dragX = 0;
    private double dragY = 0;

    private void MouseDown(MouseEventArgs obj) {
        isDragging = true;
        dragX = obj.ClientX;
        dragY = obj.ClientY;
    }

    private void MouseUp(MouseEventArgs obj) {
        isDragging = false;
    }

    private void MouseMove(MouseEventArgs obj) {
        if (!isDragging) return;

        _x += obj.ClientX - dragX;
        _y += obj.ClientY - dragY;
        dragX = obj.ClientX;
        dragY = obj.ClientY;
        StateHasChanged();
    }

}