From 8d8bd975f830c6683095cba167fa6c5633f68e7e Mon Sep 17 00:00:00 2001 From: BasavarajPS <45586075+BasavarajPS@users.noreply.github.com> Date: Tue, 16 Apr 2019 04:58:57 +0530 Subject: [PATCH] [WebView][TCSACR-219] Add Context Menu API (#674) Signed-off-by: basavarajps --- .../Interop.ChromiumEwk.ContextMenu.cs | 46 ++ .../Tizen.WebView/ContextMenu.cs | 544 ++++++++++++++++++ src/Tizen.WebView/Tizen.WebView/WebView.cs | 34 +- 3 files changed, 623 insertions(+), 1 deletion(-) create mode 100644 src/Tizen.WebView/Interop/Interop.ChromiumEwk.ContextMenu.cs create mode 100644 src/Tizen.WebView/Tizen.WebView/ContextMenu.cs diff --git a/src/Tizen.WebView/Interop/Interop.ChromiumEwk.ContextMenu.cs b/src/Tizen.WebView/Interop/Interop.ChromiumEwk.ContextMenu.cs new file mode 100644 index 000000000..9337fad2c --- /dev/null +++ b/src/Tizen.WebView/Interop/Interop.ChromiumEwk.ContextMenu.cs @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; +using System.Runtime.InteropServices; +using Tizen.WebView; + +internal static partial class Interop +{ + internal static partial class ChromiumEwk + { + [DllImport(Libraries.ChromiumEwk)] + internal static extern int ewk_context_menu_item_tag_get(IntPtr contextMenuItem); + + [DllImport(Libraries.ChromiumEwk)] + internal static extern int ewk_context_menu_item_count(IntPtr contextMenu); + + [DllImport(Libraries.ChromiumEwk)] + internal static extern IntPtr ewk_context_menu_nth_item_get(IntPtr contextMenu, int n); + + [DllImport(Libraries.ChromiumEwk)] + [return: MarshalAs(UnmanagedType.U1)] + internal static extern bool ewk_context_menu_item_remove(IntPtr contextMenu, IntPtr contextMenuItem); + + [DllImport(Libraries.ChromiumEwk)] + [return: MarshalAs(UnmanagedType.U1)] + internal static extern bool ewk_context_menu_item_append_as_action(IntPtr contextMenu, ContextMenuItemTag tag, string title, bool enabled); + + [DllImport(Libraries.ChromiumEwk)] + [return: MarshalAs(UnmanagedType.U1)] + internal static extern bool ewk_context_menu_item_append(IntPtr contextMenu, ContextMenuItemTag tag, string title, string iconPath, bool enabled); + } +} diff --git a/src/Tizen.WebView/Tizen.WebView/ContextMenu.cs b/src/Tizen.WebView/Tizen.WebView/ContextMenu.cs new file mode 100644 index 000000000..04943f85e --- /dev/null +++ b/src/Tizen.WebView/Tizen.WebView/ContextMenu.cs @@ -0,0 +1,544 @@ +/* + * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; +using System.ComponentModel; + +namespace Tizen.WebView +{ + /// + /// Enumeration for Context Menu Item Tag. + /// + /// 6 + public enum ContextMenuItemTag + { + /// + /// No action + /// + NoAction = 0, + /// + /// Open link in new window. + /// + OpenInNewWindow, + /// + /// Download link to disk. + /// + DownloadLinkToDisk, + /// + /// Copy link to clipboard. + /// + CopyLinkToClipboard, + /// + /// Open image in new window. + /// + OpenImageInNewWindow, + /// + /// Open image in current window. + /// + OpenImageInCurrentWindow, + /// + /// Download image to disk. + /// + DownloadImageToDisk, + /// + /// Copy image to clipboard. + /// + CopyImageToClipboard, + /// + /// Open frame in new window. + /// + OpenFrameInNewWindow, + /// + /// Copy. + /// + Copy, + /// + /// Go back. + /// + GoBack, + /// + /// Go forward. + /// + GoForward, + /// + /// Stop. + /// + Stop, + /// + /// Share. + /// + Share, + /// + /// Reload. + /// + Reload, + /// + /// Cut. + /// + Cut, + /// + /// Paste. + /// + Paste, + /// + /// Spelling guess. + /// + SpellingGuess, + /// + /// Guess found. + /// + NoGuessFound, + /// + /// Ignore spelling. + /// + IgnoreSpelling, + /// + /// Learn spelling. + /// + LearnSpelling, + /// + /// Other. + /// + Other, + /// + /// Search in spotlight. + /// + SearchInSpotlight, + /// + /// Search web. + /// + SearchWeb, + /// + /// Look up in dictionary. + /// + LookUpInDictionary, + /// + /// Open with default application. + /// + OpenWithDefaultApplication, + /// + /// PDF actual size. + /// + PdfActualSize, + /// + /// PDF zoom in. + /// + PdfZoomIn, + /// + /// PDF zoom out. + /// + PdfZoomOut, + /// + /// PDF auto size. + /// + PdfAutoSize, + /// + /// PDF single page. + /// + PdfSinglePage, + /// + /// PDF facting page. + /// + PdfFactingPage, + /// + /// PDF continuous. + /// + PdfContinuous, + /// + /// PDF next page. + /// + PdfNextPage, + /// + /// PDF previous page. + /// + PdfPreviousPage, + /// + /// Open link. + /// + OpenLink, + /// + /// Ignore grammar. + /// + IgnoreGrammar, + /// + /// Spelling menu. + /// + SpellingMenu, + /// + /// Show spelling panel. + /// + ShowSpellingPanel, + /// + /// Check spelling. + /// + CheckSpelling, + /// + /// Check spelling white typing. + /// + CheckSpellingWhileTyping, + /// + /// Check grammar with spelling. + /// + CheckGrammarWithSpelling, + /// + /// Font menu. + /// + FontMenu, + /// + /// Show fonts. + /// + ShowFonts, + /// + /// Bold. + /// + Bold, + /// + /// Italic. + /// + Italic, + /// + /// Underline. + /// + Underline, + /// + /// Outline. + /// + Outline, + /// + /// Style. + /// + Style, + /// + /// Show colors. + /// + ShowColors, + /// + /// Speech menu. + /// + SpeechMenu, + /// + /// Start speaking. + /// + StartSpeaking, + /// + /// Stop speaking. + /// + StopSpeaking, + /// + /// Writing direction menu. + /// + WritingDirectionMenu, + /// + /// Default direction. + /// + DefaultDirection, + /// + /// Left to right. + /// + LeftToRight, + /// + /// Right to left. + /// + RightToLeft, + /// + /// PDF single page scrolling. + /// + PdfSinglePageScrolling, + /// + /// PDF facing page scrolling. + /// + PdfFacingPageScrolling, + /// + /// Inspect element. + /// + InspectElement, + /// + /// Text direction menu. + /// + TextDirectionMenu, + /// + /// Text direction default. + /// + TextDirectionDefault, + /// + /// Text direction left to right. + /// + TextDirectionLeftToRight, + /// + /// Text direction right to left. + /// + TextDirectionRightToLeft, + /// + /// Correct spelling automatically. + /// + CorrectSpellingAutomatically, + /// + /// Substitutions menu. + /// + SubstitutionsMenu, + /// + /// Show substitutions. + /// + ShowSubstitutions, + /// + /// Smart copy paste. + /// + SmartCopyPaste, + /// + /// Smart quotes. + /// + SmartQuotes, + /// + /// Smart dashes. + /// + SmartDashes, + /// + /// Smart links. + /// + SmartLinks, + /// + /// Text replacement. + /// + TextReplacement, + /// + /// Transformation menu. + /// + TransformationMenu, + /// + /// Make upper case. + /// + MakeUpperCase, + /// + /// Make lower case. + /// + MakeLowerCase, + /// + /// Capitalize. + /// + Capitalize, + /// + /// Change back. + /// + ChangeBack, + /// + /// Open media in new window. + /// + OpenMediaInNewWindow, + /// + /// Copy media link to clipboard. + /// + CopyMediaLinkToClipboard, + /// + /// Toggle media controls. + /// + ToggleMediaControls, + /// + /// Toggle media loop. + /// + ToggleMediaLoop, + /// + /// Enter video fullscreen. + /// + EnterVideoFullscreen, + /// + /// Media play pause. + /// + MediaPlayPause, + /// + /// Media mute. + /// + MediaMute, + /// + /// Dictation alternative. + /// + DictationAlternative, + /// + /// Select all. + /// + SelectAll, + /// + /// Select word. + /// + SelectWord, + /// + /// Text selection mode. + /// + TextSelectonMode, + /// + /// Clipboard. + /// + Clipboard, + /// + /// Drag. + /// + Drag, + /// + /// Translate. + /// + Translate, + /// + /// Copy link data. + /// + CopyLinkData, + /// + /// If app want to add customized item, use enum value after BaseApplicationTag. + /// + BaseApplicationTag = 10000 + } + + /// + /// This class provides the properties of Context Menu item. + /// + /// 6 + public class ContextMenuItem + { + private IntPtr _handle; + + internal ContextMenuItem(IntPtr handle) + { + _handle = handle; + } + + internal IntPtr GetHandle() + { + return _handle; + } + + /// + /// Gets Tag of the context menu item. + /// + /// 6 + public ContextMenuItemTag ItemTag + { + get + { + return (ContextMenuItemTag) Interop.ChromiumEwk.ewk_context_menu_item_tag_get(_handle); + } + } + } + + /// + /// Arguments from the ContextMenuItem event. + /// + /// 6 + public class ContextMenuItemEventArgs : EventArgs + { + internal ContextMenuItemEventArgs(ContextMenuItem item) + { + Item = item; + } + + /// + /// Gets the context menu item. + /// + /// The context menu item. + /// 6 + public ContextMenuItem Item { get; } + + internal static ContextMenuItemEventArgs CreateFromSmartEvent(IntPtr data, IntPtr obj, IntPtr info) + { + return new ContextMenuItemEventArgs(new ContextMenuItem(info)); + } + } + + /// + /// This class provides the properties of Context Menu. + /// + /// 6 + public class ContextMenu + { + private IntPtr _handle; + + internal ContextMenu(IntPtr handle) + { + _handle = handle; + } + + /// + /// Gets the context menu items count. + /// + /// 6 + public int ItemCount + { + get + { + return Interop.ChromiumEwk.ewk_context_menu_item_count(_handle); + } + } + + /// + /// Gets Nth item of the context menu. + /// + /// To get item at index n. + /// The context menu item. + /// 6 + public ContextMenuItem GetItemAtIndex(int n) + { + IntPtr itemHandle = Interop.ChromiumEwk.ewk_context_menu_nth_item_get(_handle, n); + return new ContextMenuItem(itemHandle); + } + + /// + /// Removes item from the context menu. + /// + /// The context menu item to be removed. + /// 6 + public void RemoveItem(ContextMenuItem item) + { + IntPtr itemHandle = item.GetHandle(); + Interop.ChromiumEwk.ewk_context_menu_item_remove(_handle, itemHandle); + } + + /// + /// Appends item to the context menu. + /// + /// The tag of context menu item. + /// The title of context menu item. + /// The path of icon to be set on context menu item. + /// if true the context menu item is enabled else false. + /// Appended context menu item. + /// 6 + public ContextMenuItem AppendItem(ContextMenuItemTag tag, string title, string iconPath, bool enabled) + { + bool ret = false; + if(string.IsNullOrEmpty(iconPath)) + ret = Interop.ChromiumEwk.ewk_context_menu_item_append_as_action(_handle, tag, title, enabled); + else + ret = Interop.ChromiumEwk.ewk_context_menu_item_append(_handle, tag, title, iconPath, enabled); + + return ret ? GetItemAtIndex(ItemCount - 1) : null; + } + } + + /// + /// Arguments from the ContextMenu Item event. + /// + /// 6 + internal class ContextMenuCustomizeEventArgs : EventArgs + { + internal ContextMenuCustomizeEventArgs(ContextMenu menu) + { + Menu = menu; + } + + internal ContextMenu Menu { get; } + + internal static ContextMenuCustomizeEventArgs CreateFromSmartEvent(IntPtr data, IntPtr obj, IntPtr info) + { + return new ContextMenuCustomizeEventArgs(new ContextMenu(info)); + } + } +} diff --git a/src/Tizen.WebView/Tizen.WebView/WebView.cs b/src/Tizen.WebView/Tizen.WebView/WebView.cs index 5a11337ab..02d3f53f9 100644 --- a/src/Tizen.WebView/Tizen.WebView/WebView.cs +++ b/src/Tizen.WebView/Tizen.WebView/WebView.cs @@ -25,7 +25,7 @@ namespace Tizen.WebView /// A view used to render the web contents. /// /// 4 - public class WebView: EvasObject + public class WebView : EvasObject { private static IDictionary _javaScriptMessageHandlerMap = new Dictionary(); @@ -45,7 +45,10 @@ namespace Tizen.WebView private SmartEvent _titleChanged; private SmartEvent _urlChanged; + private SmartEvent _contextMenuItemSelected; + private SmartEvent _contextMenuCustomize; + private ContextMenuCustomize _contextMenuCustomizeDelegate; /// /// Event that occurs when the load is started. @@ -77,6 +80,19 @@ namespace Tizen.WebView /// 4 public event EventHandler UrlChanged; + /// + /// The delegate is invoked when context menu customization is needed. + /// + /// The instance of ContextMenu. + /// 6 + public delegate void ContextMenuCustomize(ContextMenu menu); + + /// + /// Event that occurs when the context menu item selected. + /// + /// 6 + public event EventHandler ContextMenuItemSelected; + /// /// Current URL of the main frame. /// @@ -372,6 +388,16 @@ namespace Tizen.WebView return _handle; } + /// + /// Sets the delegate for context menu customization. + /// + /// The delegate for context menu customization. + /// 6 + public void SetContextMenuCustomizeDelegate(ContextMenuCustomize contextMenuCustomizeDelegate) + { + _contextMenuCustomizeDelegate = contextMenuCustomizeDelegate; + } + private void InitializeSmartEvent() { // focus dummy @@ -386,12 +412,18 @@ namespace Tizen.WebView _loadError = new SmartEvent(this, _realHandle, "load,error", SmartCallbackLoadErrorArgs.CreateFromSmartEvent); _titleChanged = new SmartEvent(this, _realHandle, "title,changed", SmartCallbackArgs.CreateFromSmartEvent); _urlChanged = new SmartEvent(this, _realHandle, "url,changed", SmartCallbackArgs.CreateFromSmartEvent); + _contextMenuCustomize = new SmartEvent(this, _realHandle, "contextmenu,customize", ContextMenuCustomizeEventArgs.CreateFromSmartEvent); + _contextMenuItemSelected = new SmartEvent(this, _realHandle, "contextmenu,selected", ContextMenuItemEventArgs.CreateFromSmartEvent); _loadStarted.On += (s, e) => { LoadStarted?.Invoke(this, EventArgs.Empty); }; _loadFinished.On += (s, e) => { LoadFinished?.Invoke(this, EventArgs.Empty); }; _loadError.On += (s, e) => { LoadError?.Invoke(this, e); }; _titleChanged.On += (s, e) => { TitleChanged?.Invoke(this, e); }; _urlChanged.On += (s, e) => { UrlChanged?.Invoke(this, e); }; + + _contextMenuItemSelected.On += (s, e) => { ContextMenuItemSelected?.Invoke(this, e); }; + _contextMenuCustomize.On += (s, e) => { _contextMenuCustomizeDelegate?.Invoke(e.Menu); }; } + } } -- 2.34.1