-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathViewController.cs
More file actions
87 lines (70 loc) · 3.18 KB
/
ViewController.cs
File metadata and controls
87 lines (70 loc) · 3.18 KB
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
87
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Collections.Generic;
using UnityEngine.UIElements;
namespace Unity.Profiling.Editor.UI
{
// Base view controller class. A view controller is responsible for managing a single, logical unit of UI, known as a 'view'. View controllers may embed the views of other view controllers to form a modular hierarchy.
abstract class ViewController : IDisposable
{
// The view owned by this view controller.
VisualElement m_View;
// The view controller's child view controllers.
readonly List<ViewController> m_Children = new();
// The view controller's view. If the view does not exist when this method is called, it will be created.
public VisualElement View
{
get
{
if (m_View == null)
{
m_View = LoadView();
if (m_View == null)
throw new InvalidOperationException($"View controller did not create a view. Ensure your view controller's LoadView method returns a non-null VisualElement.");
ViewLoaded();
}
return m_View;
}
}
// Has the view controller's view been loaded?
public bool IsViewLoaded => m_View != null;
// Has this view controller been disposed?
public bool IsDisposed { get; private set; }
// Dispose this view controller. This calls Dispose on all children before removing its view from the view hierarchy.
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
// LoadView is called the first time the view controller's view is requested for display. Override this method to create the view controller's view.
protected abstract VisualElement LoadView();
// ViewLoaded is called immediately after the view controller's view has been created. Override this method to perform one-time view setup.
protected virtual void ViewLoaded() { }
// Dispose is called when the view controller is being disposed from memory. Override this method to perform one-time view clean-up. You must call the base class implementation after yours, as is standard in the C# Dispose pattern.
protected virtual void Dispose(bool disposing)
{
if (IsDisposed)
return;
if (disposing)
{
foreach (var child in m_Children)
child.Dispose();
m_View?.RemoveFromHierarchy();
m_View = null;
}
IsDisposed = true;
}
// Add viewController as a child of this view controller.
protected void AddChild(ViewController viewController)
{
m_Children.Add(viewController);
}
// Remove viewController from the children of this view controller.
protected void RemoveChild(ViewController viewController)
{
m_Children.Remove(viewController);
}
}
}