Welcome to mirror list, hosted at ThFree Co, Russian Federation.

WebViewExtraordinaire.cs - github.com/xamarin/macdoc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 63b672622d751c3c554c4475faa32bb957674ce9 (plain)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using System;
using System.Collections.Generic;
using System.Linq;
using Foundation;
using AppKit;
using WebKit;
using ObjCRuntime;

namespace macdoc
{

	public enum SwipeSide
	{
		None,
		Left,
		Right
	}
	
	public class SwipeEventArgs : EventArgs
	{
		public SwipeSide Side { get; set; }
	}
	
	public partial class WebViewExtraordinaire : WebView
	{
		public event EventHandler<SwipeEventArgs> SwipeEvent;
		public event EventHandler<SwipeEventArgs> OngoingSwipeEvent;
		
		FindBarExtraordinaireController findBarController;
		NSScrollView currentScrollView;
		
		public WebViewExtraordinaire (IntPtr handle) : base (handle)
		{
			Initialize ();
		}

		void Initialize ()
		{
			// The initializations we do here are Lion-specific
			if (!AppDelegate.IsOnLionOrBetter)
				return;
			findBarController = new FindBarExtraordinaireController ();
			IsSwipeEnabled = NSEvent.IsSwipeTrackingFromScrollEventsEnabled;
			FinishedLoad += (sender, e) => SetupFindBar (MainFrame.FrameView.DocumentView.EnclosingScrollView);
			findBarController.View.FindTextChanged += (sender, e) => InternalPerformFinderAction (NSTextFinderAction.SetSearchString);
			findBarController.View.CloseFindPanel += (sender, e) => InternalPerformFinderAction (NSTextFinderAction.HideFindInterface);
		}
		
		public bool IsSwipeEnabled { get; set; }
		
		public override void ScrollWheel (NSEvent theEvent)
		{
			if (!IsSwipeEnabled)
				return;
			if (theEvent.Phase != NSEventPhase.Began)
				return;
			if (theEvent.DeltaX == 0 || Math.Abs (theEvent.DeltaX) <= Math.Abs (theEvent.DeltaY) * 8)
				return;
			if (Math.Abs (theEvent.DeltaX) < 1f)
				return;
			
			FireSwipeEvent (theEvent.DeltaX > 0 ? SwipeSide.Left : SwipeSide.Right);
		}

		public override void SwipeWithEvent (NSEvent theEvent)
		{
			// We are only interested in horizontal swipe
			if (theEvent.DeltaX == 0)
				return;
			FireSwipeEvent (theEvent.DeltaX > 0 ? SwipeSide.Left : SwipeSide.Right);
		}
		
		public override bool WantsForwardedScrollEventsForAxis (NSEventGestureAxis axis)
		{
			return axis == NSEventGestureAxis.Horizontal && IsSwipeEnabled;
		}
	
		public override bool AcceptsFirstResponder ()
		{
			return AppDelegate.IsOnLionOrBetter;
		}
		
		void FireSwipeEvent (SwipeSide side)
		{
			var temp = SwipeEvent;
			if (temp != null)
				temp (this, new SwipeEventArgs { Side = side });
		}
		
		void FireOngoingSwipeEvent (SwipeSide status)
		{
			var temp = OngoingSwipeEvent;
			if (temp != null)
				temp (this, new SwipeEventArgs { Side = status });
		}
		
		public override void PerformFindPanelAction (NSObject sender)
		{
			if (!AppDelegate.IsOnLionOrBetter)
				return;

			var ctrl = sender as NSMenuItem;
			if (ctrl == null)
				return;
			
			InternalPerformFinderAction ((NSTextFinderAction)(int)ctrl.Tag);
		}
		
		void InternalPerformFinderAction (NSTextFinderAction action)
		{
			switch (action) {
			case NSTextFinderAction.ShowFindInterface:
				if (currentScrollView != null)
					currentScrollView.FindBarVisible = true;
				findBarController.View.GrabFocus ();
				break;
			case NSTextFinderAction.HideFindInterface:
				if (currentScrollView != null)
					currentScrollView.FindBarVisible = false;
				break;
			case NSTextFinderAction.NextMatch:
			case NSTextFinderAction.SetSearchString:
				DoSearch (true);
				break;
			case NSTextFinderAction.PreviousMatch:
				DoSearch (false);
				break;
			default:
				break;
			}
		}
		
		void SetupFindBar (NSScrollView scrollView)
		{
			currentScrollView = scrollView;
			scrollView.FindBarView = findBarController.View;
			scrollView.FindBarPosition = NSScrollViewFindBarPosition.BelowContent;
		}
		
		void DoSearch (bool forward)
		{
			var findBar = findBarController.View;
			Search (findBar.FindText, forward, findBar.CaseSensitive, findBar.Wrap);
		}
	}
}